Skip to content

Instantly share code, notes, and snippets.

@muayyad-alsadi
Created July 5, 2014 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muayyad-alsadi/324fe684d9e60fa3d227 to your computer and use it in GitHub Desktop.
Save muayyad-alsadi/324fe684d9e60fa3d227 to your computer and use it in GitHub Desktop.
primes as linked list in lua
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