Singular Integer Right Triangles¶

This problem is eerily similar to problem 39. We'll reuse our generator from that problem:

In [1]:
from itertools import count

def primitive_pythagorean_triplets(max_perim):
    for m in count(2):
        if 2*m^2 + 2*m > max_perim:
            break

        for n in range(1, m):
            if not ((m % 2) != (n % 2)) or gcd(m, n) != 1:
                continue
            
            a = m^2 - n^2
            b = 2*m*n
            c = m^2 + n^2
            
            if a + b + c > max_perim:
                break
            
            yield (a, b, c)

And we'll reuse the main loop from that problem, too!

In [2]:
from collections import Counter

limit = 1500000
lengths = Counter()

for (a, b, c) in primitive_pythagorean_triplets(limit):
    for k in count(1):
        L = k * (a + b + c)
        if L > limit:
            break
        
        lengths[L] += 1

The only differences in this problem are our maximum perimeter, and that we're looking for perimeters that can only be made from one Pythagorean triplet.

In [3]:
len({k for (k, v) in lengths.items() if v == 1})
Out[3]:
161667

Relevant sequences¶

  • Perimeters with one Pythagorean triple: A098714

Copyright (C) 2025 filifa¶

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.