Solution to problem 47 in Julia

This commit is contained in:
David Doblas Jiménez 2021-09-18 09:45:55 +02:00
parent fb87f1c9e3
commit c1119e7521

65
src/Julia/Problem047.jl Normal file
View File

@ -0,0 +1,65 @@
#=
Created on 18 Sep 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 47 of Project Euler
https://projecteuler.net/problem=47
=#
using BenchmarkTools
function factor(n)
ans = []
d = 2
while d*d <= n
if n % d == 0
push!(ans,d)
n = n ÷ d
else
d += 1
end
end
if n > 1
push!(ans,n)
end
return ans
end
function Problem47()
#=
The first two consecutive numbers to have two distinct prime factors are:
14 = 2 × 7
15 = 3 × 5
The first three consecutive numbers to have three distinct prime factors are:
644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.
Find the first four consecutive integers to have four distinct prime factors each.
What is the first of these numbers?
=#
ans = []
for number in 1:1_000_000
if length(ans) == 4
break
elseif length(Set(factor(number))) == 4
push!(ans,number)
else
ans = []
end
end
return ans[1]
end
println("Time to evaluate Problem 47:")
@btime Problem47()
println("")
println("Result for Problem 47: ", Problem47())