From e794020d8b881ede726338be50eaa461a134889f Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 20 Jun 2021 20:10:05 +0200 Subject: Refactoging julia problems with function chainning --- julia/007-10001st_prime.jl | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'julia/007-10001st_prime.jl') 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) -- cgit