blob: e2c14875c5091ef27864566ba333d82a19e46ab8 (
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
|
-- $Id: sieve.lua,v 1.9 2001/05/06 04:37:45 doug Exp $
-- http://www.bagley.org/~doug/shootout/
--
-- Roberto Ierusalimschy pointed out the for loop is much
-- faster for our purposes here than using a while loop.
local count = 0
function main(num, lim)
local flags = {}
for num=num,1,-1 do
count = 0
for i=1,lim do
flags[i] = 1
end
for i=2,lim do
if flags[i] == 1 then
k = 0
for k=i+i, lim, i do
flags[k] = 0
end
count = count + 1
end
end
end
end
NUM = tonumber((arg and arg[1])) or 100
lim = (arg and arg[2]) or 8192
print(NUM,lim)
count = 0
main(NUM, lim)
print("Count: ", count)
|