aboutsummaryrefslogtreecommitdiff
path: root/julia/010-summation_of_primes.jl
blob: 1e93a4f3bed5d557f852c66e59da889815f25c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)))