The sieve of eratosthenes

Lua

-- written by Darren Kirby

x = arg[1]
y = math.floor(math.sqrt(x))
primes = {}
set = {}
for i=2,x do
    table.insert(set, i)
end

function isFactor(index, value)
    if math.mod(value, checkint) == 0 then 
        table.remove(set, index)
    end
end

while set[1] <= y do
    table.insert(primes, set[1])
    checkint = set[1]
    table.remove(set, 1)
    for i,v in ipairs(set) do isFactor(i,v) end
end 

for key, value in primes do
    io.write(value .. " ")
end

for key, value in set do
    io.write(value .. " ")
end
print()

back to Eratosthenes page

stats

It is Saturday May 17, 2008 10:55 am
This page served 932 times
This page last modified: April 14, 2008 11:28 am
Your IP address is: 38.103.63.17
You are browsing using: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
You are browsing from: United States.
badcomputer.org's uptime: 10:55:47 up 23 days, 11:38, 0 users, load average: 0.00, 0.00, 0.00

local

home | unix stuff | dir2ogg | sneetchalizer | wmainfo | q&d guide to permissions | q&d guide to tar and gzip | code | MS rant | browser shootout | linux & iAudio X5 | photos | music | programming poetry | sieve of Eratosthenes | plea | rain | suffer | archive | about | recipes | compaqr3000 | sitemap

search

Google

credits

hacker emblem

This page, and all pages on this site were created and are maintained by Darren Kirby using valid XHTML 1.0 and CSS, and are ©copyright 2002 - 2008. The Penguin image was created by Tukka, and is used by permission. Inspiration for the look of this site was provided by Eric A. Meyer's CSS gallery. This website runs on Gentoo Linux. It is served by Apache. PHP and MySQL hold together the backend.

advertisement