1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# A history of APL in 50 functions
# But they're BQN functions
# Still about APL history though
# Also not all the functions are here yet
# https://www.jsoftware.com/papers/50/
# TODO: 13, 16, 18, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 41, 46, 47, 49, 50
# Some of the above are purely speculative snippets on the website, and may not be added to this example.
# Utilities
Words ← (⊢-˜¬×+`)∘=⟜' '⊸⊔ # Split string on spaces
Rand ← •rand.Range
# 0
ArrayLogic ← (>-<)⟜0
! 1‿¯1‿0‿1 ≡ ArrayLogic 5‿¯2.7‿0‿6
# 1
Average ← +´÷≠
! 3 ≡ Average 2‿1‿6
# 2
# x⌹x=x is an alternate way to get the average of vector x.
# There's no reason to do it this way in BQN (or APL, really).
# 3
IndexOfSelfie ← ⊐˜
! 0‿1‿2‿3‿4‿1 ≡ IndexOfSelfie "Selfie"
# 4
BarChart ← ".⎕"⊏˜>⌜⟜(↕⌈´)
! {
bc ← BarChart 3‿1‿4‿1‿5‿9
bc ≡ 6‿9⥊"⎕⎕⎕......⎕........⎕⎕⎕⎕.....⎕........⎕⎕⎕⎕⎕....⎕⎕⎕⎕⎕⎕⎕⎕⎕"
}
# 5
ParenthesesNesting ← +`1‿¯1‿0⊏˜"()"⊐⊢
! {
pn ← ParenthesesNesting "⍵((∇<S),=S,(∇>S))⍵⌷⍨?≢⍵"
pn ≡ 0‿1‿2‿2‿2‿2‿1‿1‿1‿1‿1‿2‿2‿2‿2‿1‿0‿0‿0‿0‿0‿0‿0
}
# 6
PerfectShuffle ← ⊢⊏˜·⍋∘⍒≠⥊0‿1˜
! "IAJBKCLDMENFOGPHQ" ≡ PerfectShuffle "ABCDEFGHIJKLMNOPQ"
# 7
_quicksort ← {Cmp←𝔽 ⋄ S←{p←𝕩Cmp˜𝕩⊏˜Rand≠𝕩 ⋄ ∾1‿0‿1𝕩{S⍟𝕨𝕗/˜0𝕏p}¨<‿=‿>}⍟(1<≠)}
! 1‿2‿2‿3‿5‿7‿9‿10‿10‿10‿11‿14‿16 ≡ -_quicksort 2‿2‿7‿10‿10‿11‿3‿10‿14‿5‿9‿1‿16
! {
Cmp ← ≢◶0‿(-´∘⍋∾○<)¨
srt ← Words "Anna Fi JD Jay Jd John Morten Roger Scott Zeus"
srt ≡ Cmp _quicksort Words "Fi Jay John Morten Roger JD Jd Anna Scott Zeus"
}
# 8
PascalsTriangle ← {>𝕩↑¨0(∾+∾˜)⍟(↕𝕩)⥊1}
! (>⟨1‿0‿0‿0⋄1‿1‿0‿0⋄1‿2‿1‿0⋄1‿3‿3‿1⟩) ≡ PascalsTriangle 4
! 1‿1‿2‿3‿5‿8‿13‿21‿34‿55 ≡ { 𝕩 ↑ +´¨ (+⌜˜↕𝕩) ⊔○⥊ PascalsTriangle 𝕩 } 10
# 9
GoldenRatio ← +⟜÷´¨∘(1↓↑) ⥊⟜1
! 1e¯5 ∧´∘> | (GoldenRatio 16) - 1‿2‿1.5‿1.66667‿1.6‿1.625‿1.61538‿1.61905‿1.61765‿1.61818‿1.61798‿1.61806‿1.61803‿1.61804‿1.61803‿1.61803
# 10
NewtonsMethod ← (2 ÷˜ ⊢ + ÷)´¨∘(1↓↑) ⥊
! 1e¯5 ∧´∘> | (7 NewtonsMethod 2) - 2‿1.5‿1.41667‿1.41422‿1.41421‿1.41421‿1.41421
# 11
InnerProduct ← +˝∘×⎉1‿2
! (>⟨2‿3⋄4‿1⟩) ≡ (>⟨1‿0⋄¯1‿1⟩) InnerProduct >⟨2‿3⋄6‿4⟩
# 12
CayleysTheorem ← {R←⥊⊐⊢ ⋄ (R𝕩) ≡ R ⊏˜⌜˜ <˘R𝕩}
! {
t ← 2‿2⊸⥊¨ (⌽2|⌊∘÷⟜2⍟(↕4))¨ 9‿6‿7‿11‿13‿14
g ← ≠˝∘∧⎉1‿2⌜˜t
CayleysTheorem g
}
# 14
IntervalIndex ← 1-˜≠∘⊣(⊣↓⊢⍋⊸⊏+`∘>)⍋∘∾ # Or ⍋, of course
! 0‿2‿3‿2‿4‿1‿0‿2‿¯1 ≡ ¯1‿2‿3‿7‿8.5 IntervalIndex 0‿4‿7‿6‿11‿2‿1‿3‿¯5
! 0‿1‿0‿¯1‿4‿4 ≡ "Fi Jay John Morten Roger" IntervalIndex○Words "JD Jd Geoff Anna Scott Zeus"
# 15
CentralLimit ← { 41 ↑ /⁼ (5×↕40) ⍋ +˝ Rand 𝕩⥊21 }
# 5‿8 ⥊ CentralLimit 10 1e3
# 17
SeventeenTwentyNine ← {F:
c ← 3⋆˜1+↕200
t ← (<⌜˜↕200) × +⌜˜c
d ← ⍷⊸⊐⊸⊔ ⥊t
⌊´ ⊑¨ (2=≠¨)⊸/ d
}
# ! 1729 ≡ SeventeenTwentyNine⟨⟩ # slow
# 19
Permutations ← {𝕊0:1‿0⥊0; ∾˝(0∾˘1+𝕊𝕩-1)⊸⊏˘⍒˘=⌜˜↕𝕩}
! (6‿3⥊0‿1‿2‿0‿2‿1‿1‿0‿2‿1‿2‿0‿2‿0‿1‿2‿1‿0) ≡ Permutations 3
# 20
InversePermutation ← ⍋ # Or ⊐⟜(↕≠) or ∾∘⊔
! (↕10) ≡ InversePermutation⊸⊏ 1‿4‿5‿2‿6‿8‿3‿7‿0‿9
# 21
⟨IndexFromPermutation⇐Ifp,PermutationFromIndex⇐Pfi⟩ ← {
Rfd ← ⊢+´∘>¨¯1↓↓
Dfr ← (⍋∘⍋∾)´
Ifp ⇐ {𝕊⁼:𝕨Pfi𝕩; (⌽×`1+»↕≠𝕩) +´∘× Rfd𝕩}
Pfi ⇐ {𝕊⁼:𝕨Ifp𝕩; w𝕊𝕩: Dfr (1+↕w) (⌽⊣|¯1↓·⌊∘÷`∾˜) 𝕩}
}
! {
p ← 1‿3‿0‿7‿6‿5‿4‿9‿8‿2
! p ≡ 10 PermutationFromIndex i ← IndexFromPermutation p
! p ≡ ⊢⌾(10⊸IndexFromPermutation) p
! i ≡ ⊢⌾(10⊸PermutationFromIndex) i
}
# 22
Combinations ← {𝕨(=∨0=⊣)◶⟨(0∾˘𝕊⌾(-⟜1))∾1+𝕊⟜(-⟜1), ≍∘↕⊣⟩𝕩}
! (3 Combinations 5) ≡ 10‿3⥊0‿1‿2‿0‿1‿3‿0‿1‿4‿0‿2‿3‿0‿2‿4‿0‿3‿4‿1‿2‿3‿1‿2‿4‿1‿3‿4‿2‿3‿4
# 23
⟨IndexFromCombination⇐Ic,CombinationFromIndex⇐Ci⟩ ← {
C ← ((-˜+↕∘⊣)÷○(×´1⊸+)↕∘⊣)˘ # Combination function (APL's dyadic !)
Ic ⇐ {
𝕊⁼: 𝕨Ci𝕩;
m‿n𝕊𝕩:
⊑-˝(m-↕m) +˝∘(C˘) n-(»1+𝕩)∾˘𝕩
}
Ci ⇐ {
𝕊⁼: 𝕨Ic𝕩;
0‿n𝕊𝕩: ⟨⟩;
m‿n𝕊𝕩:
v←+`(m-1)C(1-m)↓⌽↕n
k←(v>𝕩)⊐1
k∾(1+k)+(𝕨-1∾1+k)𝕊(𝕩-k⊏0∾v)
}
}
! 11 ≡ 4‿6 IndexFromCombination 1‿2‿3‿5
! 1‿2‿3‿5 ≡ 4‿6 CombinationFromIndex 11
! 11 ≡ 4‿6 CombinationFromIndex⁼ 4‿6 CombinationFromIndex 11
# 24
SymmetricArray ← ⍉⊸≡ ∧ 1⊸⍉⊸≡
! SymmetricArray +⌜´ 3⥊<2‿3‿7‿11
# 25
NQueens ← {
Arr ← {>(⊑⊸⊏⟜𝕩∾1⊸⊑)¨/○⥊⟜(↕≢)¬(↕𝕨)∊⎉1𝕩⥊∘+⎉1‿2(-⟜↕¯1⊑≢𝕩)×⌜¯1‿0‿1}
𝕩 Arr⍟(𝕩-1) ≍˘↕𝕩
}
QueenCheck ← {
! 1=≠≢𝕩
! (↕≠𝕩)≡∧𝕩
! ∧´⥊(=⌜˜↕≠𝕩)≥(|-⌜˜𝕩)=|-⌜˜↕≠𝕩
}
QueenCheck˘ NQueens 8
# 26
KnightsTour ← {
Kmoves ← {
t ← (⥊↕𝕩‿𝕩)+⌜<˘8‿2⥊2‿1‿2‿¯1‿1‿2‿1‿¯2‿¯1‿2‿¯1‿¯2‿¯2‿1‿¯2‿¯1
(∧˝⎉1(>t)∊↕𝕩) /¨○<˘ 𝕩{+⟜(𝕨⊸×)´⌽𝕩}¨t
}
m ← >↑˜¨⟜(⌈´≠¨) Kmoves 𝕩
b ← (𝕩×𝕩)⥊1
F ← {b↩0⌾(𝕩⊸⊑)b ⋄ (⊑⍋+˝˘(j⊏m)⊏b)⊑j←⊏⟜b⊸/𝕩⊏m}
𝕩‿𝕩⥊⍋F⍟(↕𝕩⋆2) 0
}
! (KnightsTour 6) ≡ 6‿6⥊0‿9‿20‿35‿6‿11‿21‿32‿7‿10‿19‿26‿8‿1‿34‿25‿12‿5‿33‿22‿31‿16‿27‿18‿2‿15‿24‿29‿4‿13‿23‿30‿3‿14‿17‿28
# 34
# Fails when 𝕩 is 1; the APL version did this too!
_pow ← {𝔽´𝔽˜⍟(/2|⌊∘÷⟜2⍟(↕·⌈2⋆⁼⊢)𝕩)𝕨}
! 847288609443 ≡ 3 ×_pow 25
# 38
Hanoi ← {¯1↓(⥊⊢≍⎉0˜≠⥊⊑⊑⟨1‿5‿2⋄0‿3‿4⟩˜)⍟𝕩⥊1}
! 0‿1‿3‿0‿4‿5‿0 ≡ Hanoi 3
# 39
Ack ← {
0 𝕊 𝕩: 1+𝕩;
𝕨 𝕊 0: (𝕨-1) 𝕊 1;
(𝕨-1) 𝕊 𝕨 𝕊 𝕩-1
}
! 9 ≡ 2 Ack 3
! 29 ≡ 3 Ack 2
# 40
f←{𝕊 _H 𝕩 ? 𝕊 𝕩; ⟨⟩}
# 42
Minors ← {(/¨≠⟜<↕≠𝕩)(⍉⊏)⎉0‿∞ 𝕩}⎉2⍟2
Minors1 ← {𝕊 mat: >{mat ⍉∘{𝕩/˜¬𝕨=↕≠𝕩}´ ⌽𝕩}¨↕≢𝕩}
! (Minors ≡ Minors1) 3‿4⥊↕12
! (Minors ≡ Minors1) 4‿4⥊'A'+↕26
# 43
S1 ← {
𝕊 0: ⋈1;
(0,t)+(t,0)× 𝕩-1⊣t←𝕊 𝕩-1
}
S2 ← {
𝕊 0: ⋈1;
(0,t)+(t,0)×↕𝕩+1⊣t←𝕊 𝕩-1
}
! ⟨ 0, 6, 11, 6, 1 ⟩ ≡ S1 4
! ⟨ 0, 6, 11, 6, 1 ⟩ ≡ S2 4
# 44
XEA←{(𝕨∾1‿0) {0=⊑𝕩 ? 𝕨; 𝕩 𝕊 𝕨-𝕩×⌊(⊑𝕨)÷⊑𝕩} (𝕩∾0‿1)}
CRT←{
m‿r 𝕊 n‿s:
gcd‿a‿b ← m XEA n
lcm ← m×n÷gcd
c ← lcm|gcd÷˜(r×b×n)+(s×a×m)
! (r=m|c)∧(s=n|c)
lcm∾c
}
! 2‿¯1‿1 ≡ 4 XEA 6
! 12‿9 ≡ 4‿1 CRT 6‿3
# 46 (not working yet)
Sieve←{
4≥𝕩 ? 𝕩⥊0‿0‿1‿1;
𝕊 n:
r←⌊√n
p ← 2‿3‿5‿7‿11‿13‿17‿19‿23‿29‿31‿37‿41‿43
p ↩ •Show (1+⊑(n≤×`p)⊐⥊1)↑p
b ← 0⌾(0⊸⊑)⋈ {(m⥊𝕩)>m⥊𝕨↑1 ⊣ m←n⌊𝕨×≢𝕩}´ ⌽1∾p
{
r<⊑b⊐⥊1 ? b⊣b ↩ 1⌾(𝕩⊸⊑)b;
q←⊑b⊐⥊1
b ↩ 0⌾((q∾q×/b↑˜⌈n÷q)⊸⊑) b
𝕊 𝕩∾q
}p
}
# 47 (given impl is wrong)
Pg ← {
d ← 𝕩+𝕩
s ← 𝕩×𝕩
t ← (𝕩∾𝕩)↑(𝕩∾d)⥊(1-d)↑(𝕩 •rand.Deal 26)⊏"abcdefghijklmnopqrstuvwxyz"
p ← ⍋(s •rand.Deal s)+(-´¨↑𝕩-˜⌽↕d)/s×↕d
(⌽↕𝕩)⌽((d-1)⥊1‿0)/⁼(𝕩∾𝕩)⥊p⊏⥊t
}
# 48
Stick ← {
c ← •rand.Range¨𝕩⥊3 # where the car is hidden
i ← •rand.Range¨𝕩⥊3 # your initial choice of door
+/c=i # number of cars that you win
}
Change ← {
c ← •rand.Range¨𝕩⥊3 # where the car is hidden
i ← •rand.Range¨𝕩⥊3 # your initial choice of door
j ← (c×i≠c)+(3|i+1+•rand.Range¨𝕩⥊2)×i=c # your changed choice
+/c=j # number of cars that you win
}
# Slow tests:
# Stick 1e6
# Change 1e6
|