Solution to problem 7 in Julia
This commit is contained in:
parent
a0c3ebee6f
commit
d137880fc6
48
src/Julia/Problem007.jl
Normal file
48
src/Julia/Problem007.jl
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#=
|
||||||
|
Created on 24 Jun 2021
|
||||||
|
|
||||||
|
@author: David Doblas Jiménez
|
||||||
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
|
Solution for Problem 7 of Project Euler
|
||||||
|
https://projecteuler.net/problem=7
|
||||||
|
=#
|
||||||
|
|
||||||
|
function is_prime(n)
|
||||||
|
if n % 2 == 0 && n > 2
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
for i in 3:round(sqrt(n) + 1)
|
||||||
|
if n % i == 0
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Problem7()
|
||||||
|
#=
|
||||||
|
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
|
||||||
|
=#
|
||||||
|
number = 2
|
||||||
|
primeList = []
|
||||||
|
while length(primeList) < 10001
|
||||||
|
if is_prime(number)
|
||||||
|
append!(primeList,number)
|
||||||
|
end
|
||||||
|
number += 1
|
||||||
|
end
|
||||||
|
ans = primeList[length(primeList)-1]
|
||||||
|
|
||||||
|
return ans
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
println("Time to evaluate Problem 7:")
|
||||||
|
@time Problem7()
|
||||||
|
println("")
|
||||||
|
println("Result for Problem 7: ", Problem7())
|
Loading…
x
Reference in New Issue
Block a user