Enumerating Primitive Pythagorean Triples

Initializing live version
Download to Desktop

Requires a Wolfram Notebook System

Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

This Demonstration generates all primitive Pythagorean triples (PPTs) using an enumeration that highlights certain meaningful patterns. Some nonprimitive Pythagorean triples are also generated; these are referred to here as "near-primitive" Pythagorean triples (NPPTs). You can filter them out by checking the "filter" checkbox. All PPTs and NPPTs are shown graphically as points on upward-opening parabolic curves, for which a simple integer-valued function is given below.


The slider sets the number of triples generated. The "complete arc" toggle either allows any integer value of or requires it to be a triangular number (always or just once), forcing the last downward-curving arc shown to be complete.

Three modes allow graphical or tabular displays of the results, including the "derivation" to show the steps from the positive integers up to to PPTs/NPPTs of the form , eventually (for large enough ) reaching any desired PPT.

In mode "graph ," a colored point represents a PPT and a gray point represents an NPPT; mouse over to see a proportional triangle, all side lengths and the intermediate indices and . All points on the same curve share the same value and curiously enough, that value is always the square of an odd positive integer: . This result is the basis of the enumeration displayed here.


Contributed by: Kenneth E. Caviness and Colton H. Davis (January 2024)
Open content licensed under CC BY-NC-SA



This Demonstration generates all primitive Pythagorean triples (PPTs) using an enumeration that highlights patterns and relationships between PPTs represented by points on upward-opening parabolas. However, like Georg Cantor’s original enumeration of all positive rational numbers (including all positive numerators and denominators), it is not one to one: some unwanted extra results are generated and can be dropped. In the case of all fractions, the unwanted duplicates reduce to the same fraction ; some nonprimitive Pythagorean triples are generated: these can be omitted or grayed out in the various display modes. These nonprimitive cases are here referred to as near-primitive Pythagorean triples (NPPTs), and all PPTs and NPPTs on a given curve are generated sequentially by simple functions of a positive integer . (The first dot in every curve corresponds to .) Another integer identifies the curve itself.

The enumeration used generates exactly one PPT candidate from every natural number, thus creating an ordered list indexed by the positive integers. This is done by first creating a two-dimensional table of ordered pairs of the form , traversed in diagonal stripes, an idea inspired by Cantor's demonstration of the countability of the rational numbers, in which the ordered pairs were . Here references the curve and point numbers. These upward-opening curves with initially mysterious gaps can be seen on any -graph of PPTs, begging for further study. When it became clear that all points on one curve shared a common value for , and that for the first curve , for the second , for the third , and so on, the current enumeration was born. We used , the curve number, to find the positive odd integer, , then gathered PPTs by . Within each subset we used the Wolfram Language function FindSequenceFunction to provide a formula to sequentially generate all PPTs on the curve. These formulas can be summarized for all :


But it is a well-known result that

is a PPT if and only if and are positive integers such that

(1) ,

(2) is odd, and

(3) and are coprime.

Comparison allows the identification of , , yielding




, and


The conditions on and now translate into conditions on and :

(1) ,

(2) is odd, and

(3) and are coprime.

The corresponding restrictions on and are:

(1) ,

(2) and are coprime.

The complete algorithm is now clear and can be studied using this Demonstration: The integer index is converted into an ordered pair , then into , then into as defined. In the "graph " mode, the formerly mysterious gaps in the curves are now seen to be legitimate Pythagorean triples, but cases where , sequentially taking on all positive integer values, is a multiple of or shares common prime factors with it. Thus, in the second curve, , every third dot is gray or omitted, according to the state of the "filter" checkbox. Hovering the mouse over any point shows a representation of the triangle, with the values , and shown. On curves with prime , every point is grayed out or omitted, but on the curve for , for instance, all points where is a multiple of either 3 or 5 (or both) are grayed out or omitted.

The "" slider controls how many candidates are generated. Use the "mode" toggle bar to change the display: graphing for PPTs and NPPTs (they appear in Cantor-diagonal order), generating a simple list or generating a commented table showing the derivation.

Snapshot 1: the scatter plot graph of 210 enumerated candidate PPTs (nonprimitive cases removed, gaps where and were not coprime clearly seen), where  is the odd leg and  the even leg of the triangle represented by the Pythagorean triple, with mouseover showing and and a proportional triangle with all side lengths

Snapshot 2: a simple list of the first 127 (N)PPTs generated by the enumeration, with nonprimitive cases grayed out

Snapshot 3: derivation of the first 28 candidates, with nonprimitive cases shown with a light gray background

Other relationships:

The points lie on the right half of upward-opening parabolas, , for all odd integers . Points appear sequentially for increasing values of , but only cases where and are coprime give PPTs. In all cases, if , then , so the triples generated by the current algorithm are precisely all -multiples of PPTs for odd , that is, the set is an odd integer and is a PPT. This set includes all PPTs (the case ), and now the definition of NPPTs is also clarified. Note that not all Pythagorean triples are generated by this enumeration, specifically, , where is a PPT but is not the square of an odd integer, is not in this list.

Considering that the set of vertical parabolas containing PPTs was such a productive notion, we might next consider the curves opening to the right. They are also half-parabolas, having the form for any positive integer . It can be seen that points on one of these curves share a common value for

: 2, 8, 18, 32, ….

A similar enumeration could be created based on which curve a point is on, then iterating over (with the added condition that be odd and coprime with respect to ) to produce the points on that curve. Interestingly enough, that generates the same graph shown here, except that points appear in the outer arc in reverse order, with points colored to highlight rightward-opening rather than upward-opening half-parabolas. It is, in fact, the same enumeration, based on Cantor's table of all rationals with all numerators and all denominators (see the references for an image), but traversing the diagonals in the opposite direction from that chosen here.

Feedback (field required)
Email (field required) Name
Occupation Organization
Note: Your message & contact information may be shared with the author of any specific Demonstration for which you give feedback.