Find Pythagorean triples (x, y, z) such that x**2 + y**2 == z**2
and x + y + z == n
.
import math
from itertools import combinations
def triples(n):
for x, y in combinations(range(1, n), 2):
z = n - x - y
if z > y and math.hypot(x, y) == z:
yield x, y, z
>>> list(triples(120))
[(20, 48, 52), (24, 45, 51), (30, 40, 50)]
>>> list(triples(3000))
[(500, 1200, 1300), (600, 1125, 1275), (750, 1000, 1250)]
>>> list(triples(12144))
[(1012, 5520, 5612), (2622, 4400, 5122), (3036, 4048, 5060), (3312, 3795, 5037), (3432, 3680, 5032)]