-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathatkin.links
221 lines (199 loc) · 6.36 KB
/
atkin.links
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
# Sieve of Atkin
#
# Computes a list of prime numbers less than or equal to some given
# upper bound.
#
# uses: lists, variants, tuples
sig max : (Int,Int) -> Int
fun max(x,y) {
if (x < y) y
else x
}
sig intersperse : (a, [a]) ~> [a]
fun intersperse(s, xs) {
switch (xs) {
case [] -> []
case [_] -> xs
case x :: xs -> x :: s :: intersperse(s, xs)
}
}
# A map implementation based on AVL trees
# (c.f. https://en.wikipedia.org/wiki/AVL_tree).
module AVLTree {
typename T(k,a) = [|Leaf
|Node:(T(k,a),k,a,T(k,a),Int)|];
sig height : (T(k,a)) ~> Int
fun height(tr) {
switch(tr) {
case Leaf -> 0
case Node(_,_,_,_,h) -> h
}
}
sig makeNode : (T(k,a),k,a,T(k,a)) ~> T(k,a)
fun makeNode(l,k,x,r) {
Node(l,k,x,r,1+max(height(l),height(r)))
}
sig member : (k, T(k,a)) ~> Bool
fun member(k, tr) {
switch (tr) {
case Leaf -> false
case Node(l,k',_,r,_) ->
if (k < k') member(k, l)
else if (k > k') member(k, r)
else true
}
}
sig lookup : (k, T(k,a)) ~> a
fun lookup(k, tr) {
switch (tr) {
case Leaf -> error("Not found")
case Node(l, k',x, r,_) ->
if (k < k') lookup(k, l)
else if (k > k') lookup(k, r)
else x
}
}
sig rotateLeft : (T(k,a)) ~> T(k,a)
fun rotateLeft(tr) {
switch (tr) {
case Node(lx, kx, x, Node(ly, ky, y, ry, _), _) ->
makeNode(makeNode(lx, kx, x, ly), ky, y, ry)
case _ -> error("Impossible [1]")
}
}
sig rotateRight : (T(k,a)) ~> T(k,a)
fun rotateRight(tr) {
switch (tr) {
case Node(Node(ly, ky, y, ry, _), kx, x, rx, _) ->
makeNode(ly, ky, y, makeNode(ry, kx, x, rx))
case _ -> error("Impossible [2]")
}
}
sig add : (k, a, T(k,a)) ~> T(k,a)
fun add(k, x, tr) {
switch (tr) {
case Leaf -> makeNode(Leaf, k, x, Leaf)
case Node(ly, ky, y, ry, h) ->
if (k < ky) {
switch (add(k, x, ly)) {
case Leaf -> error("Impossible [3]")
case Node(ly', ky', y', ry', hy') as tr' ->
if (hy' - height(ry) <= 1) makeNode(tr',ky,y,ry)
else {
var tr' = if (height(ly') < height(ry')) rotateLeft(tr')
else tr';
rotateRight(makeNode(tr', ky, y, ry))
}
}
} else if (k > ky) {
switch (add(k, x, ry)) {
case Leaf -> error("Impossible [4]")
case Node(ly', ky', y', ry', hy') as tr' ->
if (hy' - height(ly) <= 1) makeNode(ly,ky,y,tr')
else {
var tr' = if (height(ly') > height(ry')) rotateRight(tr')
else tr';
rotateLeft(makeNode(ly, ky, y, tr'))
}
}
} else {
Node(ly, ky, x, ry, h)
}
}
}
sig empty : T(k,a)
var empty = Leaf;
sig fromAssoc : (List((k,a))) ~> T(k,a)
fun fromAssoc(xs) {
fold_left(fun(tr, (k,v)) { add(k, v, tr) }, empty, xs)
}
sig toAssoc : (T(k,a)) ~> List((k,a))
fun toAssoc(tr) {
switch (tr) {
case Leaf -> []
case Node(l, k, x, r, _) ->
toAssoc(l) ++ (k, x) :: toAssoc(r)
}
}
}
# Computes a list of prime numbers less than or equal to limit.
sig atkin : (Int) ~> List(Int)
fun atkin(limit) {
# Naturals 2 and 3 are known to be prime.
var first =
if (limit >= 2) 2 :: (if (limit >= 3) [3] else []) else [];
# Initialises a storage structure with indices from 5 up to
# limit. Each component contains the boolean value false.
sig initSieve : (Int) ~> AVLTree.T(Int,Bool)
fun initSieve(limit) {
AVLTree.fromAssoc(map(fun(k) { (k, false) }, [5..limit]))
}
# Mark ps[n] is true if one of the following is True:
# a) n = 4*x^2+y^2 has odd number of solutions, i.e., there exist
# odd number of distinct pairs (x, y) that satisfy the equation and
# n mod 12 = 1 or n mod 12 = 5.
# b) n = 3*x^2+y^2 has odd number of solutions and n mod 12 = 7.
# c) n = 3*x^2-y^2 has odd number of solutions, x > y and n mod 12 =
# 11.
sig xloop : (Int, Int, Int, AVLTree.T(Int,Bool)) ~> AVLTree.T(Int,Bool)
fun xloop(x, sqrtLimit, limit, ps) {
sig yloop : (Int, Int, Int, Int, AVLTree.T(Int,Bool)) ~> AVLTree.T(Int,Bool)
fun yloop(x, y, sqrtLimit, limit, ps) {
if (y <= sqrtLimit) {
var n = (4 * x * x) + (y * y);
var ps =
if (n <= limit && (mod(n, 12) == 1 || mod(n, 12) == 5)) {
var b = AVLTree.lookup(n, ps);
AVLTree.add(n, not(b), ps)
} else ps;
var n = (3 * x * x) + (y * y);
var ps =
if (n <= limit && mod(n, 12) == 7) {
var b = AVLTree.lookup(n, ps);
AVLTree.add(n, not(b), ps)
} else ps;
var n = (3 * x * x) - (y * y);
var ps =
if (x > y && n <= limit && mod(n, 12) == 11) {
var b = AVLTree.lookup(n, ps);
AVLTree.add(n, not(b), ps)
} else ps;
yloop(x, y+1, sqrtLimit, limit, ps)
} else ps
}
if (x <= sqrtLimit) xloop( x+1, sqrtLimit, limit
, yloop(x, 1, sqrtLimit, limit, ps))
else ps
}
# Given a collection of candidate primes, this function marks
# multiples of squares as non-primes.
sig markMultiples : (Int, Int, Int, AVLTree.T(Int, Bool)) ~> AVLTree.T(Int, Bool)
fun markMultiples(i, sqrtLimit, limit, ps) {
sig innerLoop : (Int, Int, Int, AVLTree.T(Int, Bool)) ~> AVLTree.T(Int, Bool)
fun innerLoop(i, j, limit, ps) {
if (j * (i * i) <= limit) {
innerLoop(i, j + 1, limit, AVLTree.add(j * (i * i), false, ps))
} else ps
}
if (i < sqrtLimit) {
if (AVLTree.lookup(i, ps)) markMultiples( i + 1, sqrtLimit, limit
, innerLoop(i, 1, limit, ps))
else markMultiples(i + 1, sqrtLimit, limit, ps)
} else ps
}
# Precompute sqrt(limit).
var sqrtLimit = floatToInt(floor(sqrt(intToFloat(limit))));
# Check equations.
var ps = xloop(1, sqrtLimit, limit, initSieve(limit));
# Mark all mulitples of squares non-primes.
var ps = markMultiples(5, sqrtLimit, limit, ps);
# Turn the result into a list.
first ++ map((.1), filter((.2), AVLTree.toAssoc(ps)))
}
sig main : () ~> ()
fun main() {
var limit = stringToInt(getArgs() !! 0);
var primes = atkin(limit);
println(fold_right((^^), "", intersperse(", ", map(intToString, primes))))
}
main()