diff --git a/src/Julia/Problem039.jl b/src/Julia/Problem039.jl new file mode 100644 index 0000000..177fcda --- /dev/null +++ b/src/Julia/Problem039.jl @@ -0,0 +1,49 @@ +#= +Created on 09 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for Problem 39 of Project Euler +https://projecteuler.net/problem=39 +=# + +using BenchmarkTools + +function Problem39() + #= + If p is the perimeter of a right angle triangle with integral length sides, + {a,b,c}, there are exactly three solutions for p = 120: + + {20,48,52}, {24,45,51}, {30,40,50} + + For which value of p ≤ 1000, is the number of solutions maximised? + =# + ans, val = 0, 0 + for p in 2:2:1000 + sol = 0 + for a in 1:p + for b in a+1:p-2*a + c = p - (a + b) + if a^2 + b^2 == c^2 + sol += 1 + elseif a^2 + b^2 > c^2 + # As we continue our innermost loop, the left side + # gets bigger, right gets smaller, so we're done here + break + end + end + end + if sol > ans + ans, val = sol, p + end + end + + return val +end + + +println("Time to evaluate Problem 39:") +@btime Problem39() +println("") +println("Result for Problem 39: ", Problem39())