The following version uses 32-bit integers in the arrays (to reduce cache 
pressure) and 32-bit integer remainder operations, with everything else 
64-bit, and is nearly 3x faster than the original code on my machine:

rem32(x::Int32, y::Int32) = 
Base.box(Int32,Base.srem_int(Base.unbox(Int32,x),Base.unbox(Int32,y)))
function test3()
    p = Int32[0 for i=1:20000]
    result = Int32[0 for i=1:20000]   
    k = 0
    n = 2
    while k < int32(20000)
        i::Int32 = 0
        while (i < k) && ( rem32(int32(n), p[i+1]) != 0 )
            i = i + 1
        end
        if i == k
            k = k + 1
            p[k] = n            
            result[k] = n
        end
        n = n + 1
    end
    print(sum(result),"\r\n")
end

Reply via email to