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?
Edit: I realized this could actually miss some triples, see the notice on the last line.
This answer is based on the link you provided. We will use the following information:
We can use the method of Gaussian integers to find all triple generators
Any triple is a multiple of one of the above generators
To find a triple, we never need to scale a generator by less than
1/2, giving an upper bound to the biggest generator we will need.So here is some pseudocode of how we can proceed. Some details on possible implementation will follow.
The above code recovers all triple generators up to twice the provided bound. Then by scaling them up and down, we recover all triples inside our boundary.
You might want to have a look at an efficient way to find common factors to implement
find_common_factors, here is a start.Again, this implementation is based solely on the information provided in your link. Also it will catch way more triple, but might not catch triples that need to be scaled by finer fractions. There might be a more efficient way to proceed, but for that I recommend turning to MathExchange.