Files
project-euler/src/Julia/Problems001-050/Problem047.jl
David Doblas Jiménez d9c96f32cf Fix EOF
2021-10-27 11:27:14 +02:00

64 lines
1.3 KiB
Julia
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#=
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 $(lpad(47, 3, "0")):")
@btime Problem47()
println("")
println("Result for Problem $(lpad(47, 3, "0")): ", Problem47())