Created
July 5, 2014 15:43
-
-
Save muayyad-alsadi/324fe684d9e60fa3d227 to your computer and use it in GitHub Desktop.
primes as linked list in lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_primes (n) | |
compo={} | |
primes={n=nil, v=nil} | |
last=primes | |
for i=2,n do | |
if not compo[i] then | |
last.v=i | |
last.n={n=nil, n=nil} | |
last=last.n | |
for j=i*2,n,i do | |
compo[j]=true | |
end | |
end | |
end | |
return primes | |
end | |
node=get_primes(10000) | |
while node.n do | |
print(node.v) | |
node=node.n | |
end | |
t0=os.clock() | |
for i=0,500 do | |
get_primes(10000) | |
end | |
print(os.clock()-t0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment