-
Notifications
You must be signed in to change notification settings - Fork 256
/
primes.jl
180 lines (137 loc) · 3.15 KB
/
primes.jl
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
using Sockets
const UPPER_BOUND = 5_000_000
const PREFIX = 32_338
mutable struct Node
children::Dict{Char,Node}
terminal::Bool
end
Node(children::Dict) = Node(children, false)
Node() = Node(Dict(), false)
mutable struct Sieve
limit::Int64
prime::BitVector
function Sieve(limit::Int64)
return new(limit, falses(limit + 1))
end
end
function to_list(s::Sieve)::Vector{Int64}
result = [2, 3]
@inbounds for p = 1:s.limit+1
if s.prime[p] == true
push!(result, p)
end
end
return result
end
function omit_squares!(s::Sieve)
r = 5
while r^2 < s.limit
@inbounds if s.prime[r]
i = r^2
while i < s.limit
s.prime[i] = false
i += r^2
end
end
r += 1
end
end
function step1!(s::Sieve, x::Int64, y::Int64)
n = 4x^2 + y^2
if n <= s.limit && (mod(n, 12) in (1, 5))
@inbounds s.prime[n] = !s.prime[n]
end
end
function step2!(s::Sieve, x::Int64, y::Int64)
n = 3x^2 + y^2
if n <= s.limit && mod(n, 12) == 7
@inbounds s.prime[n] = !s.prime[n]
end
end
function step3!(s::Sieve, x::Int64, y::Int64)
n = 3x^2 - y^2
if x > y && n <= s.limit && mod(n, 12) == 11
@inbounds s.prime[n] = !s.prime[n]
end
end
function loop_y!(s::Sieve, x::Int64)
y = 1
while y^2 < s.limit
step1!(s, x, y)
step2!(s, x, y)
step3!(s, x, y)
y += 1
end
end
function loop_x!(s::Sieve)
x = 1
while x^2 < s.limit
loop_y!(s, x)
x += 1
end
end
function calc(s::Sieve)
loop_x!(s)
omit_squares!(s)
return s
end
function generate_trie(prime_list::Vector{Int64})::Node
root = Node()
for p in prime_list
head = root
for ch in string(p)
if !(ch in keys(head.children))
head.children[ch] = Node()
end
head = head.children[ch]
end
head.terminal = true
end
return root
end
function find(upper_bound::Int64, search_prefix::Int64)::Vector
primes = calc(Sieve(upper_bound))
str_prefix = string(search_prefix)
head = generate_trie(to_list(primes))
for char in str_prefix
head = get(head.children, char, nothing)
head == nothing && return nothing
end
queue = [(head, str_prefix)]
result = Int64[]
while !isempty(queue)
top, prefix = pop!(queue)
if top.terminal == true
push!(result, parse(Int64, prefix))
end
for (char, v) in top.children
pushfirst!(queue, (v, prefix * char))
end
end
sort!(result)
return result
end
function notify(msg)
try
socket = connect("localhost", 9001)
write(socket, msg)
close(socket)
catch
# standalone usage
end
end
function verify()
left = [2, 23, 29]
right = find(100, 2)
if left != right
println("$left != $right")
exit()
end
end
if abspath(PROGRAM_FILE) == @__FILE__
verify()
notify("Julia\t$(getpid())")
results = find(UPPER_BOUND, PREFIX)
notify("stop")
println(results)
end