From 20c35a6140364a8148581549db28987e75f88006 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Tue, 14 Sep 2021 18:55:33 +0200 Subject: [PATCH] Solution to problem 44 in Julia --- src/Julia/Problem044.jl | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/Julia/Problem044.jl diff --git a/src/Julia/Problem044.jl b/src/Julia/Problem044.jl new file mode 100644 index 0000000..d7a5e88 --- /dev/null +++ b/src/Julia/Problem044.jl @@ -0,0 +1,51 @@ +#= +Created on 14 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 44 of Project Euler +https://projecteuler.net/problem=44 +=# + +using BenchmarkTools +using Combinatorics + +function pentagonal(n) + return Int(n*(3*n-1)/2) +end + +function Problem44() + #= + Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. + The first ten pentagonal numbers are: + + 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... + + It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their + difference, 70 − 22 = 48, is not pentagonal. + + Find the pair of pentagonal numbers, Pj and Pk, for which their + sum and difference are pentagonal and D = |Pk − Pj| is minimised. + + What is the value of D? + =# + dif = 0 + pentagonal_list = [pentagonal(n) for n in 1:2500] + pairs = combinations(pentagonal_list, 2) + for p in pairs + if reduce(+, p) in pentagonal_list && abs(reduce(-, p)) in pentagonal_list + dif = (abs(reduce(-,p))) + # the first one found would be the smallest + break + end + end + + return dif +end + + +println("Time to evaluate Problem 44:") +@btime Problem44() +println("") +println("Result for Problem 44: ", Problem44())