[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/IWI7F58z    [ raw code | output | fork ]

programmingpraxis - Lua, pasted on Jun 24:
-- sieve of eratosthenes

function primes (n)

    max_index = math.floor((n-1)/2)

    v = {}
    for i = 1, max_index do
        v[i] = true
    end

    p = 3
    while p*p <= n do
        i = math.floor(p/2)
        if v[i] then
            for j = 2*i*(i+1), max_index, p do
                v[j] = false
            end
        end
        p = p + 2
    end

    ps = {2}
    for i = 1, max_index do
        if v[i] then
            table.insert(ps, 2*i+1)
        end
    end

    return ps

end

print(#(primes(1000)))


Output:
1
168


Create a new paste based on this one


Comments: