# Project Euler Problem 39 Solution

## Question

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$.

$\displaystyle \{20,48,52\}, \{24,45,51\}, \{30,40,50\}$

For which value of $p \leq 1000$, is the number of solutions maximised?

import Data.Function (on)
import Data.List (maximumBy)

solutions :: Int -> [(Int, Int, Int)]
solutions p = [(a, b, c) | a <- [1..p quot 4],
let b = (p * (p - 2*a)) quot (2 * (p - a)),
let c = floor $sqrt$ fromIntegral (a^2 + b^2),
a + b + c == p]

main :: IO ()
main = print $maximumBy (compare on (length . solutions)) [2,4..1000] $ ghc -O2 -o right-triangles right-triangles.hs
$time ./right-triangles real 0m0.004s user 0m0.000s sys 0m0.000s ## Python #!/usr/bin/env python from collections import defaultdict def calculate_solutions(limit): res = defaultdict(list) for a in range(1, limit+1): for b in range(a, limit+1): for c in range(b, limit+1): if a**2 + b**2 == c**2: res[a+b+c].append((a,b,c)) return res def main(): solutions = calculate_solutions(500) most = max(len(s) for s in list(solutions.values())) for p, s in sorted(solutions.items()): if len(s) == most: print(p) if __name__ == "__main__": main() $ time python3 triangle-solve.py
real   0m13.254s
user   0m13.248s
sys    0m0.004s

## Ruby

#!/usr/bin/env ruby
solutions = {}
(1..500).each do |a|
(a..500).each do |b|
(b..500).each do |c|
if a**2 + b**2 == c**2
solutions[a+b+c] ||= 0
solutions[a+b+c] += 1
end
end
end
end
puts solutions.max { |a,b| a[1] <=> b[1] }[0]
\$ time ruby triangle-solve.rb
real   0m3.062s
user   0m3.052s
sys    0m0.008s
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 5, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}