From ad6c4e4cd25bcba490c5fd1b7d3efbdf57480b1e Mon Sep 17 00:00:00 2001 From: daviddoji Date: Mon, 20 Sep 2021 17:29:35 +0200 Subject: [PATCH] Solution to problem 50 in Julia --- src/Julia/Problem050.jl | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Julia/Problem050.jl diff --git a/src/Julia/Problem050.jl b/src/Julia/Problem050.jl new file mode 100644 index 0000000..e890591 --- /dev/null +++ b/src/Julia/Problem050.jl @@ -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()) \ No newline at end of file