Read More | www.bee-man.us | 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).
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.
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.
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!!
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: