aboutsummaryrefslogtreecommitdiff
path: root/examples/fifty.bqn
blob: a4e3462a0d2b76942eeba436c6bb3384c6fe8988 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
# 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
# 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¯101  ArrayLogic 5¯2.706

# 1
Average  +´÷≠
! 3  Average 216

# 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  ˜
! 012341  IndexOfSelfie "Selfie"

# 4
BarChart  ".⎕"˜>(↕⌈´)
! {
  bc  BarChart 314159
  bc  69"⎕⎕⎕......⎕........⎕⎕⎕⎕.....⎕........⎕⎕⎕⎕⎕....⎕⎕⎕⎕⎕⎕⎕⎕⎕"
}

# 5
ParenthesesNesting  +`1¯10˜"()"⊐⊢
! {
  pn  ParenthesesNesting "⍵((∇<S),=S,(∇>S))⍵⌷⍨?≢⍵"
  pn  01222211111222210000000
}

# 6
PerfectShuffle  ⊢⊏˜·⍒≠⥊01˜
! "IAJBKCLDMENFOGPHQ"  PerfectShuffle "ABCDEFGHIJKLMNOPQ"

# 7
_quicksort  {Cmp𝔽  S{p𝕩Cmp˜𝕩˜Rand≠𝕩  101𝕩{S𝕨𝕗/˜0𝕏p}¨<=>}(1<≠)}
! 1223579101010111416  -_quicksort 2271010113101459116
! {
  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}
! (>1000110012101331)  PascalsTriangle 4
! 11235813213455  { 𝕩  +´¨ (+⌜˜𝕩)  PascalsTriangle 𝕩 } 10

# 9
GoldenRatio  +÷´¨(1↓↑) 1
! 1e¯5 ´> | (GoldenRatio 16) - 121.51.666671.61.6251.615381.619051.617651.618181.617981.618061.618031.618041.618031.61803

# 10
NewtonsMethod  (2 ÷˜  + ÷)´¨(1↓↑) 
! 1e¯5 ´> | (7 NewtonsMethod 2) - 21.51.416671.414221.414211.414211.41421

# 11
InnerProduct  +˝×12
! (>2341)  (>10¯11) InnerProduct >2364

# 12
CayleysTheorem  {R⥊⊐⊢  (R𝕩)  R ˜⌜˜ <˘R𝕩}
! {
  t  22¨ (2|⌊÷2(4))¨ 967111314
  g  ˝12⌜˜t
  CayleysTheorem g
}

# 14
IntervalIndex  1-˜(⊣↓⊢⍋⊏+`>)  # Or ⍋, of course
! 02324102¯1  ¯12378.5 IntervalIndex 047611213¯5
! 010¯144  "Fi Jay John Morten Roger" IntervalIndexWords "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:100; ˝(0˘1+𝕊𝕩-1)˘˘=⌜˜𝕩}
! (63012021102120201210)  Permutations 3

# 20
InversePermutation    # Or ⊐⟜(↕≠) or ∾∘⊔
! (10)  InversePermutation 1452683709

# 21
IndexFromPermutationIfp,PermutationFromIndexPfi  {
  Rfd  ⊢+´>¨¯1↓↓
  Dfr  (⍋∾)´
  Ifp  {𝕊:𝕨Pfi𝕩; (⌽×`1+»↕≠𝕩) +´× Rfd𝕩}
  Pfi  {𝕊:𝕨Ifp𝕩; w𝕊𝕩: Dfr (1+↕w) (⌽⊣|¯1·÷`˜) 𝕩}
}
! {
  p  1307654982
  ! p  10 PermutationFromIndex i  IndexFromPermutation p
  ! p  (10IndexFromPermutation) p
  ! i  (10PermutationFromIndex) i
}

# 22
Combinations  {𝕨(=∨0=⊣)(0˘𝕊(-1))1+𝕊(-1), ↕⊣𝕩}
! (3 Combinations 5)  103012013014023024034123124134234

# 23
IndexFromCombinationIc,CombinationFromIndexCi  {
  C  ((-˜+↕)÷(×´1+))0  # Combination function (APL's dyadic !)
  Ic  {
    𝕊: 𝕨Ci𝕩;
    mn𝕊𝕩:
    ⊑-˝(m-↕m) +˝(C˘) n-(»1+𝕩)˘𝕩
  }
  Ci  {
    𝕊: 𝕨Ic𝕩;
    0n𝕊𝕩: ⟨⟩;
    mn𝕊𝕩:
    v+`(m-1)C(1-m)↓⌽↕n
    k(v>𝕩)1
    k(1+k)+(𝕨-11+k)𝕊(𝕩-k0v)
  }
}
! 11  46 IndexFromCombination 1235
! 1235  46 CombinationFromIndex 11
! 11  46 CombinationFromIndex 46 CombinationFromIndex 11

# 24
SymmetricArray    1
! SymmetricArray +⌜´ 3⥊<23711

# 25
NQueens  {
  Arr  {>(𝕩1)¨/(↕≢)¬(𝕨)1𝕩+12(-¯1⊑≢𝕩)ׯ101}
  𝕩 Arr(𝕩-1) ˘𝕩
}
QueenCheck  {
  ! 1=≠≢𝕩
  ! (↕≠𝕩)≡∧𝕩
  ! ´(=⌜˜↕≠𝕩)(|-⌜˜𝕩)=|-⌜˜↕≠𝕩
}
QueenCheck˘ NQueens 8

# 26
KnightsTour  {
  Kmoves  {
    t  (⥊↕𝕩𝕩)+<˘82212¯1121¯2¯12¯1¯2¯21¯2¯1
    (˝1(>t)∊↕𝕩) /¨<˘ 𝕩{+(𝕨×)´𝕩}¨t
  }
  m  >↑˜¨(´¨) Kmoves 𝕩
  b  (𝕩×𝕩)1
  F  {b0(𝕩)b  (⊑⍋+˝˘(jm)b)jb/𝕩m}
  𝕩𝕩⥊⍋F(𝕩2) 0
}
! (KnightsTour 6)  6609203561121327101926813425125332231162718215242941323303141728

# 34
# Fails when 𝕩 is 1; the APL version did this too!
_pow  {𝔽´𝔽˜(/2|⌊÷2(·2)𝕩)𝕨}
! 847288609443  3 ×_pow 25

# 38
Hanoi  {¯1(⥊⊢≍0˜≠⥊⊑⊑152034˜)𝕩1}
! 0130450  Hanoi 3

# 39
Ack  {
  0 𝕊 𝕩: 1+𝕩;
  𝕨 𝕊 0: (𝕨-1) 𝕊 1;
  (𝕨-1) 𝕊 𝕨 𝕊 𝕩-1
}
! 9  2 Ack 3
! 29  3 Ack 2

# 40
# If _H solves the halting problem (𝔽 _H 𝕩 is 1 iff 𝔽𝕩 halts) then the
# following function causes a paradox.
# F←{𝕊 _H 𝕩 ? 𝕊 𝕩; ⟨⟩}

# 42
Minors  {(/¨<↕≠𝕩)(⍉⊏)0 𝕩}22
Minors1  {𝕊 mat: >{mat {𝕩/˜¬𝕨=↕≠𝕩}´ 𝕩}¨↕≢𝕩}
! (Minors  Minors1) 34⥊↕12
! (Minors  Minors1) 44'A'+↕26

# 43
S1  {
  𝕊 0: 1;
  (0t)+(t0)× 𝕩-1t𝕊 𝕩-1
}
S2  {
  𝕊 0: 1;
  (0t)+(t0)×↕𝕩+1t𝕊 𝕩-1
}
!  0, 6, 11, 6, 1   S1 4
!  0, 1,  7, 6, 1   S2 4

# 44
XEA  {(𝕨10) {0=⊑𝕩 ? 𝕨; 𝕩 𝕊 𝕨-𝕩×⌊(𝕨)÷⊑𝕩} (𝕩01)}
CRT  {
  mr 𝕊 ns:
  gcdab  m XEA n
  lcm  m×n÷gcd
  c  lcm|gcd÷˜(r×b×n)+(s×a×m)
  ! (r=m|c)(s=n|c)
  lcmc
}
! 2¯11  4 XEA 6
! 129  41 CRT 63

# 46
Sieve{
  4𝕩 ? 𝕩0011;
  𝕊 n:
  r⌊√n
  p  235711131719232931374143
  p  (1+(×`p)<n-1)p
  b  0(1) 1 {mn𝕨×≠𝕩  (m𝕩)>m𝕨1}´ p
  {
    rqb<1 ?
    b 0¨((qq×/b˜n÷q))
    𝕊 𝕩q
  ;
    b 1¨(𝕩)
  }p
}
! 2357111317192329  /Sieve 30
! 78498  +´Sieve 1e6

# 47
Pg  •rand.Deal{
  d  𝕩+𝕩
  s  𝕩×𝕩
  t  (𝕩𝕩)(𝕩d)(1-d)(𝕩𝔽26)"abcdefghijklmnopqrstuvwxyz"
  p  (s𝔽s)+(-´¨1↓↑𝕩-˜⌽↕d)/s×↕d
  (⌽↕𝕩)˘((d-1)10){𝕩(𝕨/)' '¨𝕨}˘(𝕩𝕩)p⊏⥊t
}
#•Show Pg¨ 4‿5‿6‿7
TestPg  {n𝕊p:
  ! (>(«∨»)(n)1(⊢∾∾)0˜n-1)  ' 'p
  rows  (<' '/)˘p
  ! ´ (<"") (»(∧≡↕)¨) rows
  ! ´ ¯1rows
}
TestPgPg¨ 4567

# 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×ic)+(3|i+1+•rand.Range¨𝕩2)×i=c  # your changed choice
 +´c=j                                    # number of cars that you win
}
#•Show Stick‿Change {𝕎𝕩}¨ 1e6

# 49
# +´(1+↕∞)⋆-s ←→ ×´÷1-(⍭↕∞)⋆-s