Pythagorean Triple Calculator
A JavaScript web page for finding Pythagorean Triples.
©2006, Bob Beeman
Read More Important Notice

Pythagorean Triples are sets of 3 positive integers where a2 + b2 = h2. They are so called because the Pythagorean Theorem tells us that if the lengths of the sides of a triangle are proportional to the numbers in such a triple, then the triangle is a right triangle with "a" and "b" being the lengths of the legs and "h" being the length of the hypotenuse.

The JavaScript calculator below finds Pythagorean Triples either by increasing length of the shortest leg, or by increasing length of the hypotenuse. The algorithms for these two operations are different - it is not just a different sort, because that would be computationally inefficient. Euclid's Algorithm is used to eliminate triples (such as 6, 8, 10) which are simply multiples of others (3, 4, 5).

Pythagorean Triple Calculator
Action Begin End Display

The algorithm starts from the "begin" value and terminates after the "end" value is searched. Default values are provided. If you leave the end value blank, only the value for the begin number will be searched, just as if you were to put the same number in both boxes.

For output display you can have nicely spaced columns (the default) or tab-delimited output, which can be directly pasted into spreadsheets. Changing the format is very fast because only the output is changed, the calculations are not re-done. In either case, the first number on the line is an arbitrary line number (1..n), then the shortest leg, then the longer leg, and finally the hypotenuse.

These calculations can be very computationally intensive. On any computer that can display this page the default calculations take only a few seconds. But remember that the only way to terminate a long run is to force-quit your browser (sorry: that's how JavaScript works), so use caution.

This page has been tested on my iMac with Safari, Netscape, and Internet Explorer. Netscape 7.2 is outrageously slow. Safari is probably the best. Nevertheless, this should work in any modern browser on any OS (Linux, Mac, Windows, etc).

Read More
This calculator solves for Pythagorean Triples in which three integers have the relationship: a2 + b2 = h2.

Euclid's Formula (circa 300 bc) generates all possible primitive Pythagorean Triples (not multiples of other triples) from two coprime numbers "m" and "n" if m > n and at least one of them is even. It may be necessary to swap "a" and "b".

a = m2 - n2
b = 2mn
h = m2 + n2

JavaScript uses IEEE double-precision floating point to represent numbers, and its accuracy is consistent with that representation. IEEE double precision floating point uses an exponent of 12 bits, a sign bit, and a mantissa of 51 bits. Thus integers up to about 251 or 2 x 1015 can be represented without roundoff errors. This should be borne in mind when running integer calculations on very large numbers.

As with any web page written in JavaScript, you can see how I have coded the routines by doing a "View Source" from your browser.

Finding triples in order of the shortest leg.

To find triples in order of the shortest leg, we start with the beginning value entered and count upward, one at a time. For example, if we started with 3, we would search for all Triples that have 3 for their shortest leg, then for all with 4 as the shortest let, etc. etc.

Once we have chosen a trial value for "a", the first and smallest member of the triple, we need to consider how far up we need to search for "b", the second number of the triple. From the definition of a Pythagorean Triple we know that "a", "b", and "h" must all be integers. So for any given value of "a", we are looking for the lowest value of "b" for which a2 is less than the difference between b2 and (b + 1)2.

This value is important. Failing to search values up to this limit will cause us to miss some triples. Searching values of "b" above this is a waste of time, since above this limit a2 is smaller than the minimum required to reach the next square.

We can do a little algebra and find that (b+1)2 = b2 + 2b + 1. So when "b" becomes so large that 2b + 1 is larger than a2, there is no further point in searching for larger values of "b", as a2 is no longer large enough for the sum of the two squares to even reach (b + 1)2.

So our limit for "b" is the next integer larger (to be safe) than a2/2.

It is important to note that after finding a value for "b" that produces a triple we cannot abandon the present value for "a", since there may be some larger value of "b" that combines with "a" to produce a larger "h". So we must continue on until the limit for "b" (as described above) has been reached. At that point we have finished with that value of "a" and can move on to a+1.

Example: If "a" = 20 then we must check all values of "b" up to a2/2 = 202/2 = 200
202 + 212 = 292
202 + 992 = 1012

An even better example:
602 + 912 = 1092
602 + 2212 = 2292
602 + 8992 = 9012

An illustration of the upper limit for "b", which in this case is a2/2 = 652/2 = 2112.5
652 + 722 = 972
652 + 21122 = 21132

The algorithm continues chugging along until all values of "a" in the range requested have been searched. At that point it is time to output the triples that have been found. These are stored in global JavaScript variables as they are found, and printed out as the last step in either Column or Tab Delimited form from the stored variables.


If you have followed the previous argument you can see that in the above calculation, b can be as large as a2/2 and therefore in calculating "h", the hypotenuse, this upper value of "b" must be squared. This means that numbers as large as a4/4 can result. This implies that the upper limit of "a" is 9457 (because 94574/4 = 1.9996 x 1015 which is near the upper range of integers that can be represented accurately by JavaScript). Therefore you should probably not use this calculator for numbers larger than that. Of course the practicalities of run time on real computers will also be a factor at that level [;-)

Finding triples in order of the shortest hypotenuse.

This is simpler and faster than finding triples associated with the lowest leg. One reason for this is that for any trial hypotenuse, defining the value of one leg defines value of the other. So the procedure is to choose the trial hypotenuse and step through values of the shorter leg. if the two legs are "a" and "b" and the hypotenuse is "h", then we step through the possible values for "a" and calculate what the corresponding value for "b" would be. For example:
a2 + b2 = h2 and therfore b2 = h2 - a2.

So for each candidate value of "a" we find the candidate value for "b". If the value of "b" is an integer, and it has no common factors with "a", we have found a triple.

Of course we have to check for all the values of "a" up to h/sqrt(2). It is not necessary to search above that, as once "a" goes above that, the value of "b" would be smaller than the value of "a" and we would just find duplicates.

Happy Computing!!

This page is copyrighted "freeware"
©2006, Bob Beeman
That means that although it is copyrighted, it is intended for you to use for education or entertainment. You may use it yourself, copy and redistribute it, or even put it on your own website. I ask only that you not make any changes. If you reuse any of the code, make sure to list me as one of your sources.

My only reward for writing this is the 15 milliseconds of fame I receive from having my name here. Don't deprive me of that.

You can copy this page by simply doing a "Save As" in your browser and putting it somewhere on your hard drive (or your web site). If you stop there the background will be gone. To preserve the background, copy the following file into this same folder, without changing its name, by again using your browser's "Save As". The next time you refresh the page, the background should be restored:


I make NO guarantee of any kind.
This page may contain serious errors.
Use this page entirely at your own risk!