Finding all the Pythagorean triples

May 03, 2013 21:59

My interest was sparked by this Quora question: How would you find all the Pythagorean triplets in an array of N numbers? The first answer was the straightforward O(N^2) examination of all pairs of numbers. It seemed like there might be a better way. One of the comments initially suggested O(N^2) was probably the best possible, because of its ( Read more... )

algorithm, theory, complexity, programming

Leave a comment

Comments 3

barking_iguana May 16 2013, 04:59:35 UTC
As you may know, there is a one-to-tone correspondence between irreducible Pythagorean Triples and sets of Natural Numbers {M, N}, where M > N, M and N are mutually prime and one of them is even. The sets where M and N have common factors or are both odd generate triples that are reducible. The Legs are M^2 - N^2 and 2MN. The Hypotenuse is M^2 + N^ 2.

Is that useful in limiting the search space?

Reply

markgritter May 17 2013, 02:24:31 UTC
I thought about that, it seems like it should give an alternate way of enumerating all the primitive pythagorean triples (with bounded hypotenuse), although I do not know if it is possible to enumerate the M and N efficiently due the relative primality constraint (for example, if we have to run gcd for every possible pair?)

I did not see any way to apply it to the search in a more direct fashion.

Reply

barking_iguana September 23 2013, 21:13:13 UTC
Iterate on factors?

Reply


Leave a comment

Up