aboutsummaryrefslogtreecommitdiff
path: root/julia/007-10001st_prime.jl
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-06-20 20:10:05 +0200
committerCharles Cabergs <me@cacharle.xyz>2021-06-20 20:10:05 +0200
commite794020d8b881ede726338be50eaa461a134889f (patch)
tree99f32380378fa6813d7ccf15b56d8b48fcd263c2 /julia/007-10001st_prime.jl
parent16a3e5fc6728f1c0d414983f6e1fc3fc160034b3 (diff)
downloadproject_euler-e794020d8b881ede726338be50eaa461a134889f.tar.gz
project_euler-e794020d8b881ede726338be50eaa461a134889f.tar.bz2
project_euler-e794020d8b881ede726338be50eaa461a134889f.zip
Refactoging julia problems with function chainning
Diffstat (limited to 'julia/007-10001st_prime.jl')
-rw-r--r--julia/007-10001st_prime.jl24
1 files changed, 13 insertions, 11 deletions
diff --git a/julia/007-10001st_prime.jl b/julia/007-10001st_prime.jl
index 6928484..70bf4e3 100644
--- a/julia/007-10001st_prime.jl
+++ b/julia/007-10001st_prime.jl
@@ -10,6 +10,8 @@
using Printf
using Base.Iterators
+# Can't use eratosthenes sieve since we don't know when to stop (no predefined list of numbers)
+# A algorithm that caches the previous found primes should be faster
function is_prime(n)
if n == 2 || n == 3 || n == 5
return true
@@ -17,7 +19,7 @@ function is_prime(n)
if n % 2 == 0 || n % 3 == 0 || n % 5 == 0
return false
end
- for d in 6:6:Int64(ceil(sqrt(n)) + 1)
+ for d in 6:6:ceil(Integer, √n) + 1
if n % (d - 1) == 0 || n % (d + 1) == 0
return false
end
@@ -25,13 +27,13 @@ function is_prime(n)
true
end
-counter = 0
-for n in Iterators.countfrom(2)
- if is_prime(n)
- global counter += 1
- @printf "%5d: %d\n" counter n
- end
- if counter == 10_001
- break
- end
-end
+const PRIME_INDEX = 10_001
+
+result = (
+ Iterators.countfrom(2)
+ |> x -> Iterators.filter(is_prime, x)
+ |> x -> Iterators.take(x, PRIME_INDEX)
+ |> collect
+)[end]
+
+println(result)