Solution to problem 50 in Julia
This commit is contained in:
parent
bcabf48bc3
commit
ad6c4e4cd2
54
src/Julia/Problem050.jl
Normal file
54
src/Julia/Problem050.jl
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#=
|
||||||
|
Created on 20 Sep 2021
|
||||||
|
|
||||||
|
@author: David Doblas Jiménez
|
||||||
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
|
Solution for Problem 50 of Project Euler
|
||||||
|
https://projecteuler.net/problem=50
|
||||||
|
=#
|
||||||
|
|
||||||
|
using BenchmarkTools
|
||||||
|
using Primes
|
||||||
|
|
||||||
|
function Problem50()
|
||||||
|
#=
|
||||||
|
The prime 41, can be written as the sum of six consecutive primes:
|
||||||
|
|
||||||
|
41 = 2 + 3 + 5 + 7 + 11 + 13
|
||||||
|
|
||||||
|
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
|
||||||
|
|
||||||
|
The longest sum of consecutive primes below one-thousand that adds to a prime,
|
||||||
|
contains 21 terms, and is equal to 953.
|
||||||
|
|
||||||
|
Which prime, below one-million, can be written as the sum of the most consecutive primes?
|
||||||
|
=#
|
||||||
|
|
||||||
|
ans = 0
|
||||||
|
result = 0
|
||||||
|
prime_list = primes(1_000_000)
|
||||||
|
|
||||||
|
for i in 1:length(prime_list)
|
||||||
|
sum = 0
|
||||||
|
count = 0
|
||||||
|
for j in prime_list[i:end]
|
||||||
|
sum += j
|
||||||
|
count += 1
|
||||||
|
if isprime(sum) && count > result
|
||||||
|
result = count
|
||||||
|
ans = sum
|
||||||
|
end
|
||||||
|
if sum > 1_000_000
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return ans
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
println("Time to evaluate Problem 50:")
|
||||||
|
@btime Problem50()
|
||||||
|
println("")
|
||||||
|
println("Result for Problem 50: ", Problem50())
|
Loading…
x
Reference in New Issue
Block a user