The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
I really should start looking for alternatives to brute-forcing prime detection. Perhaps something iterative as prime decomposition to avoid too much checking. Perhaps next time… ;)
function isPrime(n::UInt64)
isPrimeNumber::Bool = true;
if n%2 == 0
isPrimeNumber = false;
return isPrimeNumber
end
for i in 3:2:(n-1)
if n%i == 0
isPrimeNumber = false;
return isPrimeNumber
end
end
return isPrimeNumber
end
function sumOfPrimesBelowThreshold(N::UInt64)
sumOfPrimes::UInt64 = 0;
if N >= 2
sumOfPrimes = 2;
end
currentNumber::UInt64 = 3
while currentNumber < N
if isPrime(currentNumber)
sumOfPrimes += currentNumber;
end
currentNumber += 2;
end
return sumOfPrimes
end
function parseARGS()
if isempty(ARGS)
N::UInt64 = 2000000;
else
N = tryparse(UInt64,ARGS[1]);
end
return N
end
function main()
N::UInt64 = parseARGS();
sumOfPrimes = sumOfPrimesBelowThreshold(N);
println(sumOfPrimes);
end
main()
$ julia ProjectEuler0010.jl 10
17
0.000040 seconds (17 allocations: 912 bytes)
$ julia ProjectEuler0010.jl 100
1060
0.000042 seconds (19 allocations: 944 bytes)
$ julia ProjectEuler0010.jl 1000
76127
0.000221 seconds (19 allocations: 944 bytes)
$ julia ProjectEuler0010.jl 2000000
142913828922
323.662743 seconds (19 allocations: 944 bytes)
There has to be a faster way for this.