From c1119e75219bcc919d3555a24c91d5aa7173e178 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sat, 18 Sep 2021 09:45:55 +0200 Subject: [PATCH] Solution to problem 47 in Julia --- src/Julia/Problem047.jl | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Julia/Problem047.jl diff --git a/src/Julia/Problem047.jl b/src/Julia/Problem047.jl new file mode 100644 index 0000000..99765ba --- /dev/null +++ b/src/Julia/Problem047.jl @@ -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())