Solution to problem 37 in Julia
This commit is contained in:
parent
2213718a30
commit
b116ca9e18
53
src/Julia/Problem037.jl
Normal file
53
src/Julia/Problem037.jl
Normal file
@ -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())
|
Loading…
x
Reference in New Issue
Block a user