Solution to problem 35 in Julia

This commit is contained in:
David Doblas Jiménez 2021-09-02 16:25:15 +02:00
parent 1cf3cc68ce
commit 1def60b631

72
src/Julia/Problem035.jl Normal file
View File

@ -0,0 +1,72 @@
#=
Created on 02 Sep 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 35 of Project Euler
https://projecteuler.net/problem=35
=#
using BenchmarkTools
using Combinatorics
using Primes
# function circular_number(n)
# result = []
# perms = collect(permutations(digits(n), length(n)))
# for i in perms
# push!(result, parse(Int, join(i)))
# end
# return result
# end
function circular_number(n)
if n <10
return [n]
end
digs=digits(n)
d=length(digs)
cyc=zeros(Int,d)
x=[10^i for i in 0:d-1]
for i in 1:d
cyc[i]=sum(x .* digs[vcat(i:d,1:i-1)])
end
return cyc
end
function Problem35()
#=
The number, 197, is called a circular prime because all rotations of the
digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100:
2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
=#
circular_primes = []
cnt = 0
for i in 2:1_000_000
if isprime(i)
all_primes = true
for j in circular_number(i)
if !isprime(j)
all_primes = false
break
end
end
if all_primes
cnt +=1 #push!(circular_primes, i)
end
end
end
return cnt #length(circular_primes)
end
println("Time to evaluate Problem 35:")
@btime Problem35()
println("")
println("Result for Problem 35: ", Problem35())