diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-06-18 21:50:31 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-06-18 21:50:31 +0200 |
| commit | 93b4c2a215556153ed948260678cd8e840493b39 (patch) | |
| tree | 393edc821a5cd00db7971b4612c98855ff762945 /julia/007-10001st_prime.jl | |
| parent | c0aeed4578bdaea39ddbed1b55896948f56b23f3 (diff) | |
| download | project_euler-93b4c2a215556153ed948260678cd8e840493b39.tar.gz project_euler-93b4c2a215556153ed948260678cd8e840493b39.tar.bz2 project_euler-93b4c2a215556153ed948260678cd8e840493b39.zip | |
problem 3 4 5 6 6 in julia
Diffstat (limited to 'julia/007-10001st_prime.jl')
| -rw-r--r-- | julia/007-10001st_prime.jl | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/julia/007-10001st_prime.jl b/julia/007-10001st_prime.jl new file mode 100644 index 0000000..6928484 --- /dev/null +++ b/julia/007-10001st_prime.jl @@ -0,0 +1,37 @@ +### +# 10001st prime +# Problem 7 +# +# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th +# prime is 13. +# What is the 10 001st prime number? +### + +using Printf +using Base.Iterators + +function is_prime(n) + if n == 2 || n == 3 || n == 5 + return true + end + if n % 2 == 0 || n % 3 == 0 || n % 5 == 0 + return false + end + for d in 6:6:Int64(ceil(sqrt(n)) + 1) + if n % (d - 1) == 0 || n % (d + 1) == 0 + return false + end + end + 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 |
