Question
If is the perimeter of a right angle triangle with integral length sides, , there are exactly three solutions for .
For which value of , is the number of solutions maximised?
Haskell
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.003s
user 0m0.000s
sys 0m0.003s
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()