I have only just recently found out about a way of generating Pythagorean triples through this video explaining it, involving the use of Gaussian (complex) integers. I have so far managed to write a function returning a list of Pythagorean triples generated by every Gaussian integer where the imaginary part is less than the real part.
def pyt(max_real):
t = []
real = 2
imag = 1
while real <= max_real:
z = complex(real, imag)**2
t.append((z.real, z.imag, abs(z)))
if imag + 1 == real:
real += 1
imag = 1
else:
imag += 1
return t
The problem with this is that some triplets (such as {9, 12, 15}) are not generated through the initial step in the video that the function has been based on, and I'm unsure of how to generate those.
>>> for i in pyt(4):
print(i)
(3.0, 4.0, 5.0)
(8.0, 6.0, 10.0)
(5.0, 12.0, 13.0)
(15.0, 8.0, 17.0)
(12.0, 16.0, 20.0)
(7.0, 24.0, 25.0)
>>> # missing: (9, 12, 15), possibly others
How would I go about generating every possible triplet, somehow using the ones I already have or otherwise?