diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-06-19 12:17:16 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-06-19 12:17:16 +0200 |
| commit | 16a3e5fc6728f1c0d414983f6e1fc3fc160034b3 (patch) | |
| tree | 2dc9ba1f40d96378040f60ed36fa589c3e81b566 /julia/010-summation_of_primes.jl | |
| parent | 51464d5a71c4550683940db2753c50db0569bc17 (diff) | |
| download | project_euler-16a3e5fc6728f1c0d414983f6e1fc3fc160034b3.tar.gz project_euler-16a3e5fc6728f1c0d414983f6e1fc3fc160034b3.tar.bz2 project_euler-16a3e5fc6728f1c0d414983f6e1fc3fc160034b3.zip | |
problem 8 9 10 in julia
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))) |
