This commit is contained in:
David Doblas Jiménez
2021-10-27 11:27:14 +02:00
parent 846a9343ae
commit d9c96f32cf
51 changed files with 315 additions and 366 deletions

View File

@@ -5,14 +5,13 @@ Created on 14 Sep 2021
@email: daviddoji@pm.me
Solution for Problem 44 of Project Euler
https://projecteuler.net/problem=44
=#
https://projecteuler.net/problem=44 =#
using BenchmarkTools
using Combinatorics
function pentagonal(n)
return Int(n*(3*n-1)/2)
return Int(n * (3 * n - 1) / 2)
end
function Problem44()
@@ -28,14 +27,13 @@ function Problem44()
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?
=#
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)))
dif = (abs(reduce(-, p)))
# the first one found would be the smallest
break
end
@@ -45,7 +43,7 @@ function Problem44()
end
println("Time to evaluate Problem 44:")
println("Time to evaluate Problem $(lpad(44, 3, "0")):")
@btime Problem44()
println("")
println("Result for Problem 44: ", Problem44())
println("Result for Problem $(lpad(44, 3, "0")): ", Problem44())