Solution to problem 39 in Julia

This commit is contained in:
David Doblas Jiménez 2021-09-09 15:58:37 +02:00
parent d35117e417
commit a6cdae898a

49
src/Julia/Problem039.jl Normal file
View File

@ -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())