diff options
Diffstat (limited to 'julia/010-summation_of_primes.jl')
| -rw-r--r-- | julia/010-summation_of_primes.jl | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/julia/010-summation_of_primes.jl b/julia/010-summation_of_primes.jl new file mode 100644 index 0000000..1e93a4f --- /dev/null +++ b/julia/010-summation_of_primes.jl @@ -0,0 +1,23 @@ +### +# Summation of primes +# Problem 10 +# +# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. +# Find the sum of all the primes below two million. +### + +function eratosthenes_sieve(stop) + ns = Array(reverse(2:stop)) + primes = [] + while true + prime = pop!(ns) + push!(primes, prime) + if prime > ceil(sqrt(stop)) + return append!(primes, ns) + break + end + ns = filter(n -> n % prime != 0, ns) + end +end + +println(sum(eratosthenes_sieve(2_000_000 - 1))) |
