-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathprimes.lua
197 lines (170 loc) · 3.8 KB
/
primes.lua
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
package.cpath = package.cpath .. ';../common/libnotify/target/?.so'
local has_libnotify, libnotify = pcall(require, 'lua_libnotify')
local UPPER_BOUND = 5000000
local PREFIX = 32338
local function Node()
return {
children = {},
terminal = false,
}
end
local function Sieve(limit)
local fill_array = function(size, el)
a = {}
for i = 1, size do
a[i] = el
end
return a
end
local prime = fill_array(limit + 1, false)
local to_list = function()
local result = {2, 3}
for p = 5, limit do
if prime[p] then
table.insert(result, p)
end
end
return result
end
local omit_squares = function()
local r = 5
while r * r < limit do
if prime[r] then
local i = r * r
while i < limit do
prime[i] = false
i = i + r * r
end
end
r = r + 1
end
end
local step1 = function(x, y)
local n = (4 * x * x) + (y * y)
if n <= limit and (n % 12 == 1 or n % 12 == 5) then
prime[n] = not prime[n]
end
end
local step2 = function(x, y)
local n = (3 * x * x) + (y * y)
if n <= limit and n % 12 == 7 then
prime[n] = not prime[n]
end
end
local step3 = function(x, y)
local n = (3 * x * x) - (y * y)
if x > y and n <= limit and n % 12 == 11 then
prime[n] = not prime[n]
end
end
local loop_y = function(x)
local y = 1
while y * y < limit do
step1(x, y)
step2(x, y)
step3(x, y)
y = y + 1
end
end
local loop_x = function()
local x = 1
while x * x < limit do
loop_y(x)
x = x + 1
end
end
local calc = function()
loop_x()
omit_squares()
end
return {
calc = calc,
to_list = to_list,
}
end
local function Queue(list)
list.first = 1
list.last = #list
local push = function(value)
local last = list.last + 1
list.last = last
list[last] = value
end
local pop = function()
local first = list.first
if first > list.last then
error("list is empty")
end
local value = list[first]
list[first] = nil
list.first = first + 1
return value
end
local empty = function()
return list.first > list.last
end
return {
push = push,
pop = pop,
empty = empty,
}
end
local function generate_trie(l)
local root = Node()
for _, el in ipairs(l) do
local head = root
local str = tostring(el)
for i = 1, #str do
local ch = str:sub(i, i)
if not head.children[ch] then
head.children[ch] = Node()
end
head = head.children[ch]
end
head.terminal = true
end
return root
end
local function dump(arr)
return "[" .. table.concat(arr, ", ") .. "]"
end
local function find(upper_bound, prefix)
local primes = Sieve(upper_bound)
primes.calc()
local str_prefix = tostring(prefix)
local head = generate_trie(primes.to_list())
for ch in str_prefix:gmatch"." do
head = head.children[ch]
if not head then
return nil
end
end
local queue, result = Queue({{head, str_prefix}}), {}
while not queue.empty() do
local top, prefix = table.unpack(queue.pop())
if top.terminal then
table.insert(result, tonumber(prefix))
end
for ch, v in pairs(top.children) do
queue.push({v, prefix .. ch})
end
end
table.sort(result)
return result
end
local function verify()
local left = dump({2, 23, 29})
local right = dump(find(100, 2))
if left ~= right then
io.stderr:write(string.format("%s != %s", left, right))
os.exit(false)
end
end
(function()
verify()
local compiler = type(jit) == 'table' and "Lua/luajit" or "Lua"
if has_libnotify then libnotify.notify_with_pid(compiler) end
local results = find(UPPER_BOUND, PREFIX)
if has_libnotify then libnotify.notify("stop") end
print(dump(results))
end)()