(related)
A Pythagorean Triple is a list (a, b, c)
that satisfies the equation a2 + b2 = c2.
A Primitive Pythagorean Triple (PPT) is one where a
, b
, and c
are all coprime (i.e., the only common divisor between the three elements is 1
). For example, the (3, 4, 5)
right triangle is a famous Primitive Pythagorean Triple.
The Challenge
- Given input
n
, output then
th PPT. Or, - Given input
n
, output the firstn
PPTs.
There are multiple ways to order these PPTs to form a well-ordered list, to determine which is the n
th. You can choose any ordering you want, so long as you can prove (informally is fine) that your algorithm can generate every possible unique PPT. For example, your code should not output both (3,4,5)
and (4,3,5)
since those are duplicates of the same triple -- one or the other, please.
Similarly, whether your code is zero- or one-indexed is fine, so long as you state which you're using.
Examples
For the examples below, I'm using one-indexing, outputting the n
th PPT, and ordering by smallest c
, then smallest a
, then smallest b
.
n | output
1 | (3, 4, 5)
2 | (5, 12, 13)
5 | (20, 21, 29)
12| (48, 55, 73)
Rules
- The input and output can be given in any convenient format.
- In your submission, please state how your entries are ordered, and whether your entries are 0-indexed or 1-indexed.
- Your chosen ordering cannot create duplicates.
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- If possible, please include a link to an online testing environment so other people can try out your code!
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.