Skip to content

Instantly share code, notes, and snippets.

@matux
Created February 26, 2019 11:31
Show Gist options
  • Save matux/c185970772cd7da9207ce6577c01d56d to your computer and use it in GitHub Desktop.
Save matux/c185970772cd7da9207ce6577c01d56d to your computer and use it in GitHub Desktop.
Quadratic Primes in APL
Dyalog APL/S-64 Version 17.0.35664
Unicode Edition
Tue Feb 26 00:37:36 2019
⍝ _Little did he know that the simple, seemingly innocuous act of starting
⍝ Dyalog's APL REPL would result in his imminent institutionalization._
⍝ Some array-driven, some fp, some quadratic, some primes.
gfx.∆.Init
⎕IO←0
⎕IO
0
⍳20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
X←⍳20
X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
X∘.|X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1
0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4 5
0 1 2 3 4 5 6 7 8 9 10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2 4 3 4 2 6 2 4 4 5 2 6 2
2=+⌿0=X∘.|X
0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
(2=+⌿0=X∘.|X)⌿X ⍝ filter X
2 3 5 7 11 13 17 19
filter←{(αα¨⍵)⌿⍵}
2=+⌿0=X∘.|X
0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
⍉X,⍪2=+⌿0=X∘.|X ⍝ bit vector
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
⍪2=+⌿0=X∘.|X
0
0
1
1
0
1
0
1
0
0
0
1
0
1
0
0
0
1
0
1
⍴⍪2=+⌿0=X∘.|X ⍝ Table it
20 1
X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
X,⍪2=+⌿0=X∘.|X ⍝ Table it
0 0
1 0
2 1
3 1
4 0
5 1
6 0
7 1
8 0
9 0
10 0
11 1
12 0
13 1
14 0
15 0
16 0
17 1
18 0
19 1
⍉X,⍪2=+⌿0=X∘.|X ⍝ Transpose
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
2=+⌿0=X∘.|X
0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2↓X
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Y←2↓X
Y
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
~Y∊Y∘.×Y ⍝ ys not a product of themselves
1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
Y∘.×Y
4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57
8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114
14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133
16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152
18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171
20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190
22 33 44 55 66 77 88 99 110 121 132 143 154 165 176 187 198 209
24 36 48 60 72 84 96 108 120 132 144 156 168 180 192 204 216 228
26 39 52 65 78 91 104 117 130 143 156 169 182 195 208 221 234 247
28 42 56 70 84 98 112 126 140 154 168 182 196 210 224 238 252 266
30 45 60 75 90 105 120 135 150 165 180 195 210 225 240 255 270 285
32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304
34 51 68 85 102 119 136 153 170 187 204 221 238 255 272 289 306 323
36 54 72 90 108 126 144 162 180 198 216 234 252 270 288 306 324 342
38 57 76 95 114 133 152 171 190 209 228 247 266 285 304 323 342 361
Y~Y∘.×Y ⍝ ys not a product of themselves
2 3 5 7 11 13 17 19
(⊢~∘.×⍨)
┌──┼──┐
⊢ ~ ⍨
┌──┘
.
┌─┴─┐
∘ ×
Y
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(⊢~∘.×⍨)Y
2 3 5 7 11 13 17 19
Y~∘.×⍨Y
2 3 5 7 11 13 17 19
⍉X,⍪2=+⌿0=X∘.|X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
2=+⌿0=X∘.|X
0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1
⍳2=+⌿0=X∘.|X
LIMIT ERROR: Rank of resultant array would exceed maximum permitted
⍳2=+⌿0=X∘.|X
⍸2=+⌿0=X∘.|X
2 3 5 7 11 13 17 19
residue←|
⍸2=+⌿0=X∘.residue X
2 3 5 7 11 13 17 19
⍝ where 2 equals plus reduce_first 0 equals X outer_product residue of X
⍝ ⍸ 2 = + ⌿ 0 = X ∘. | X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment