From b116ca9e188279e89095f9496fcd68cc80ee97c3 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Tue, 7 Sep 2021 16:52:08 +0200 Subject: [PATCH] Solution to problem 37 in Julia --- src/Julia/Problem037.jl | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/Julia/Problem037.jl diff --git a/src/Julia/Problem037.jl b/src/Julia/Problem037.jl new file mode 100644 index 0000000..da5dae2 --- /dev/null +++ b/src/Julia/Problem037.jl @@ -0,0 +1,53 @@ +#= +Created on 07 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 37 of Project Euler +https://projecteuler.net/problem=37 +=# + +using BenchmarkTools +using Primes + +function is_truncatable_prime(number) + num_str_rev = reverse(digits(number)) + + for (idx,num) in enumerate(join(num_str_rev)) + if !isprime(parse(Int, join(num_str_rev[idx:end]))) || + !isprime(parse(Int, join(num_str_rev[1:end-idx+1]))) + return false + end + end + return true +end + +function Problem37() + """ + The number 3797 has an interesting property. Being prime itself, it is + possible to continuously remove digits from left to right, and remain + prime at each stage: 3797, 797, 97, and 7. + Similarly we can work from right to left: 3797, 379, 37, and 3. + + Find the sum of the only eleven primes that are both truncatable from left + to right and right to left. + + NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. + """ + ans = 0 + # Statement of the problem says this + primes_list = primes(11, 1_000_000) + for num in primes_list + if is_truncatable_prime(num) + ans += num + end + end + return ans +end + + +println("Time to evaluate Problem 37:") +@btime Problem37() +println("") +println("Result for Problem 37: ", Problem37())