A Binary-Based Enumeration of the 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 the Calkin–Wilf enumeration of the rational numbers. The main slider selects , the number of PPTs shown. Various modes allow graphical or tabular displays of the results, including "derivation" to show the steps from the positive integers , to a filtered list of "compliant" natural numbers, to pairs, to the PPTs of the form . In graph modes, either or can be displayed, with points identified by mouseover or traditional labels, and the "zoom" slider zooms in on the part of the graph near the origin.

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


This enumeration of all primitive Pythagorean triples (PPTs) generates exactly one PPT from every natural number, thus creating a comprehensive ordered list of all PPTs, indexed by the natural numbers. This is done by first taking the function , which generates a list consisting of only those natural numbers that in turn will generate compliant Calkin–Wilf (C–W) pairs of the form , subject to the conditions that and be relatively prime, have opposite parity and that . These pairs are then used to generate PPTs of the form , thus giving a one-to-one mapping between the set of all natural numbers and the set of all PPTs.

The sliders control how many PPTs are calculated, and in graph mode how much to zoom in on those close to the origin. Use the "mode" toggle bar to select between graphing the PPTs or the C–W pairs, or generating a simple list or a commented table of PPTs.

Snapshot 1: the scatter plot graph of the enumerated PPTs as given on the plane, where is the odd leg and the even leg of the triangle represented by the PPT, zoomed in to show only of the largest value

Snapshot 2: a simple list of the PPTs generated by the enumeration

Snapshot 3: the scatter plot graph of the compliant C–W pairs (which can also be thought of as reduced fractions) used to generate the PPTs, shown in the plane. In this context, compliant means that and satisfy three criteria:

(1) and are relatively prime

(2) and are not both odd


These are the conditions needed to give only PPTs.

Snapshot 4: a simple list of the compliant C–W pairs used to generate the PPTs

Snapshot 5: explanatory table giving all steps of the process: natural numbers, compliant natural numbers, compliant C–W pairs, and PPTs generated by the enumeration algorithm

Most generating algorithms for PPTs are based on a ternary tree (see references for examples), while the present algorithm, suggested in [1, Section 8], by Kevin Ryde, and similar to the procedure sketched out in section "Enumeration of Primitive Pythagorean Triples" of [1], relies on a binary tree, in this case the C–W tree of reduced rational numbers. Therefore condition (1) is automatically guaranteed for any index. We found that condition (3) was guaranteed by eliminating all odd index numbers (whose binary form ends with 1), and (2) by omitting 4 and every third remaining index thereafter, leaving as allowed indices. The function gives precisely that set, providing only the natural numbers that generate compliant C–W pairs as needed to generate only PPTs. It is worth noting that removing these restrictions does not generate all non-primitive Pythagorean triples.


[1] Wikipedia. "Pythagorean Triple." (Mar 10, 2023) en.wikipedia.org/wiki/Pythagorean_triple.

[2] Wikipedia. "Tree of Primitive Pythagorean Triples." (Mar 10, 2023) en.wikipedia.org/wiki/Tree_of_primitive_Pythagorean_triples.

[3] Wikipedia. "Calkin–Wilf Tree." (Mar 10, 2023) en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree.

[4] A. Hall, "Genealogy of Pythagorean Triads," The Mathematical Gazette, 54(390), 1970, pp. 377–379. doi:10.2307/3613860.

[5] H. Lee Price, "The Pythagorean Tree: A New Species." arxiv.org/abs/0809.4324.

[6] H. Andres Lönnemo. "The Trinary Tree(s) underlying Primitive Pythagorean Triples." (Mar 10, 2023) www.cut-the-knot.org/pythagoras/PT_matrix.shtml.

[7] K. Ryde, "Trees of Primitive Pythagorean Triples." unpublished manuscript, last modified May 8, 2020. download.tuxfamily.org/user42/triples/triples.pdf.


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.