All Euler problems
Project Euler

Singular Integer Right Triangles

Given that L is the perimeter of a right triangle with integer sides, for how many values of L <= 1,500,000 can exactly one integer-sided right triangle be formed?

Source sync May 21, 2026
Problem #0075
Level Level 03
Solved By 20,792
Languages C++, Python
Answer 161667
Length 488 words
number_theorygeometrymodular_arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

It turns out that $12 \operatorname{cm}$ is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples. \begin{align*} 12 \operatorname{cm}: (3, 4, 5) \\ 24 \operatorname{cm}: (6, 8, 10) \\ 30 \operatorname{cm}: (5, 12, 13) \\ 36 \operatorname{cm}: (9, 12, 15) \\ 40 \operatorname{cm}: (8, 15, 17) \\ 48 \operatorname{cm}: (12, 16, 20) \\ \end{align*} In contrast, some lengths of wire, like $20 \operatorname{cm}$, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using $120 \operatorname{cm}$ it is possible to form exactly three different integer sided right angle triangles. $$120 \operatorname{cm}: (30, 40, 50), (20, 48, 52), (24, 45, 51)$$ Given that $L$ is the length of the wire, for how many values of $L \le 1\,500\,000$ can exactly one integer sided right angle triangle be formed?

Problem 75: Singular Integer Right Triangles

Mathematical Analysis

Theorem 1 (Euclid’s Parametrization of Primitive Triples)

Every primitive Pythagorean triple (a,b,c)(a, b, c) with a2+b2=c2a^2 + b^2 = c^2, a,b,c>0a, b, c > 0, and gcd⁡(a,b,c)=1\gcd(a, b, c) = 1 can be written (up to swapping aa and bb) as:

a=m2−n2,b=2mn,c=m2+n2a = m^2 - n^2, \quad b = 2mn, \quad c = m^2 + n^2

where m>n>0m > n > 0, gcd⁡(m,n)=1\gcd(m, n) = 1, and m≢n(mod2)m \not\equiv n \pmod{2}. Conversely, every such pair (m,n)(m, n) generates a primitive triple.

Proof.

(⇐)(\Leftarrow) Given valid (m,n)(m, n), direct computation verifies:

a2+b2=(m2−n2)2+(2mn)2=m4−2m2n2+n4+4m2n2=(m2+n2)2=c2.a^2 + b^2 = (m^2 - n^2)^2 + (2mn)^2 = m^4 - 2m^2n^2 + n^4 + 4m^2n^2 = (m^2 + n^2)^2 = c^2.

For primitivity: suppose a prime pp divides gcd⁡(a,b)\gcd(a, b). Then p∣(m2−n2)p \mid (m^2 - n^2) and p∣2mnp \mid 2mn. Since mm and nn have opposite parity, a=m2−n2a = m^2 - n^2 is odd, so p≠2p \neq 2. Then from p∣2mnp \mid 2mn and pp odd, p∣mp \mid m or p∣np \mid n. If p∣mp \mid m, then from p∣m2−n2p \mid m^2 - n^2 we get p∣n2p \mid n^2, hence p∣np \mid n, contradicting gcd⁡(m,n)=1\gcd(m, n) = 1. Similarly for p∣np \mid n. Thus gcd⁡(a,b)=1\gcd(a, b) = 1, and since gcd⁡(a,b)=1\gcd(a, b) = 1 implies gcd⁡(a,b,c)=1\gcd(a, b, c) = 1 (as c2=a2+b2c^2 = a^2 + b^2), the triple is primitive.

(⇒)(\Rightarrow) Let (a,b,c)(a, b, c) be a primitive triple with bb even (exactly one of a,ba, b must be even; if both were odd, then c2=a2+b2≡2(mod4)c^2 = a^2 + b^2 \equiv 2 \pmod{4}, which is impossible since squares are ≡0\equiv 0 or 1(mod4)1 \pmod{4}). Factor:

b2=c2−a2=(c−a)(c+a).b^2 = c^2 - a^2 = (c - a)(c + a).

Since aa is odd and cc is odd (as c2=a2+b2c^2 = a^2 + b^2 with aa odd and bb even), both c−ac - a and c+ac + a are even. Write c−a=2sc - a = 2s, c+a=2tc + a = 2t, so b2=4stb^2 = 4st and (b/2)2=st(b/2)^2 = st.

Claim: gcd⁡(s,t)=1\gcd(s, t) = 1. Indeed, if d∣sd \mid s and d∣td \mid t, then d∣(t−s)=ad \mid (t - s) = a and d∣(t+s)=cd \mid (t + s) = c, so d∣gcd⁡(a,c)d \mid \gcd(a, c). Since gcd⁡(a,c)=1\gcd(a, c) = 1 (because gcd⁡(a,b,c)=1\gcd(a, b, c) = 1 and any common factor of a,ca, c would divide b2b^2 hence bb), we get d=1d = 1.

Since st=(b/2)2st = (b/2)^2 is a perfect square and gcd⁡(s,t)=1\gcd(s, t) = 1, both ss and tt must be perfect squares: s=n2s = n^2, t=m2t = m^2. Then a=t−s=m2−n2a = t - s = m^2 - n^2, c=t+s=m2+n2c = t + s = m^2 + n^2, b=2mnb = 2mn, with m>n>0m > n > 0 (since c>a>0c > a > 0), gcd⁡(m,n)=1\gcd(m, n) = 1 (from gcd⁡(s,t)=1\gcd(s, t) = 1), and m≢n(mod2)m \not\equiv n \pmod{2} (since a=m2−n2a = m^2 - n^2 is odd). □\square

Theorem 2 (Perimeter Formula)

The perimeter of the primitive triple generated by (m,n)(m, n) is L0=2m(m+n)L_0 = 2m(m + n).

Proof.

L0=a+b+c=(m2−n2)+2mn+(m2+n2)=2m2+2mn=2m(m+n).□L_0 = a + b + c = (m^2 - n^2) + 2mn + (m^2 + n^2) = 2m^2 + 2mn = 2m(m + n). \quad \square

Theorem 3 (Scaling Principle)

Every Pythagorean triple (a,b,c)(a, b, c) is uniquely expressible as k⋅(a0,b0,c0)k \cdot (a_0, b_0, c_0) for a positive integer kk and a primitive triple (a0,b0,c0)(a_0, b_0, c_0). Consequently, perimeter LL admits a Pythagorean triple if and only if L=kL0L = kL_0 for some primitive perimeter L0L_0 and some k≥1k \geq 1.

Proof. Setting g=gcd⁡(a,b,c)g = \gcd(a, b, c), the triple (a/g,b/g,c/g)(a/g, b/g, c/g) is primitive and (a,b,c)=g⋅(a/g,b/g,c/g)(a, b, c) = g \cdot (a/g, b/g, c/g). Uniqueness: if k(a0,b0,c0)=k′(a0′,b0′,c0′)k(a_0, b_0, c_0) = k'(a_0', b_0', c_0') with both primitive, then k=k′k = k' (since gcd⁡(a0,b0,c0)=gcd⁡(a0′,b0′,c0′)=1\gcd(a_0, b_0, c_0) = \gcd(a_0', b_0', c_0') = 1) and the triples coincide. □\square

Lemma (Bound on mm)

For primitive perimeters L0=2m(m+n)≤Lmax⁡L_0 = 2m(m+n) \leq L_{\max} with n≥1n \geq 1, we have m<Lmax⁡/2m < \sqrt{L_{\max}/2}.

Proof. From 2m(m+n)≤Lmax⁡2m(m + n) \leq L_{\max} and n≥1n \geq 1: 2m(m+1)≤Lmax⁡2m(m+1) \leq L_{\max}, so m2<m(m+1)≤Lmax⁡/2m^2 < m(m+1) \leq L_{\max}/2. For Lmax⁡=1,500,000L_{\max} = 1{,}500{,}000, this gives m<750000≈866.03m < \sqrt{750000} \approx 866.03, i.e., m≤866m \leq 866. □\square

Definition (Singular Perimeter)

A perimeter LL is called singular if there is exactly one Pythagorean triple (counting different orderings of legs as the same triple) with perimeter LL.

Theorem 4 (Counting Method)

For each perimeter L≤Lmax⁡L \leq L_{\max}, define T(L)T(L) as the number of distinct Pythagorean triples with perimeter LL. Then T(L)=∑L0∣L1[L0 is a primitive perimeter]T(L) = \sum_{L_0 \mid L} \mathbf{1}[L_0 \text{ is a primitive perimeter}]. The answer is ∣{L≤Lmax⁡:T(L)=1}∣|\{L \leq L_{\max} : T(L) = 1\}|.

Proof. By Theorem 3, each Pythagorean triple with perimeter LL corresponds to a unique factorization L=kL0L = kL_0 with L0L_0 primitive and k≥1k \geq 1. Distinct primitive perimeters L0L_0 dividing LL give distinct triples. □\square

Editorial

Euclid’s parametrization gives a clean generation scheme for all primitive triples. Every valid pair (m,n)(m,n) with opposite parity and gcd⁡(m,n)=1\gcd(m,n)=1 produces exactly one primitive perimeter

L0=2m(m+n).L_0 = 2m(m+n).

That already handles candidate generation. The filtering comes from the Euclid conditions: pairs with the same parity or a common factor do not produce primitive triples, so we discard them immediately. Once a primitive perimeter is known, every multiple of it corresponds to a non-primitive triple, so we mark all multiples in a counter array. After the sweep, a perimeter is singular precisely when its counter is equal to one.

Pseudocode

Create a counter array for all perimeters up to the limit.
Compute the largest necessary value of m.

For each m starting from 2:
    For each n with 1 <= n < m:
        If m and n have the same parity, skip this pair
        If gcd(m, n) is greater than 1, skip this pair

        Compute the primitive perimeter L0 = 2m(m + n)
        If L0 is above the limit, stop increasing n for this m

        For every multiple of L0 within the limit:
            increase the corresponding perimeter counter

Count how many perimeters were hit exactly once.
Return that count.

Complexity Analysis

Outer loop: The iterations over (m,n)(m, n) pairs total ∑m=2mmax⁡(m−1)=O(mmax⁡2)=O(Lmax⁡)\sum_{m=2}^{m_{\max}} (m-1) = O(m_{\max}^2) = O(L_{\max}) before filtering.

Inner loop: For each primitive perimeter L0L_0, we mark ⌊Lmax⁡/L0⌋\lfloor L_{\max}/L_0 \rfloor multiples. The total work is:

∑primitive L0≤Lmax⁡Lmax⁡L0.\sum_{\text{primitive } L_0 \leq L_{\max}} \frac{L_{\max}}{L_0}.

By analogy with the sum of reciprocals over a sieved set, this is O(Lmax⁡log⁡log⁡Lmax⁡)O(L_{\max} \log \log L_{\max}).

  • Time: O(Lmax⁡log⁡log⁡Lmax⁡)O(L_{\max} \log \log L_{\max}).
  • Space: O(Lmax⁡)O(L_{\max}) for the counter array.

Answer

161667\boxed{161667}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_075/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Count singular perimeters L <= 1,500,000.
    // Generate primitive Pythagorean triples via Euclid: L0 = 2m(m+n).
    // Mark all multiples kL0. Count perimeters hit exactly once.

    const int LMAX = 1500000;
    vector<int> cnt(LMAX + 1, 0);

    int mlimit = (int)sqrt((double)LMAX / 2.0) + 1;

    for (int m = 2; m <= mlimit; m++) {
        for (int n = 1; n < m; n++) {
            if ((m - n) % 2 == 0) continue;
            if (__gcd(m, n) != 1) continue;

            int L0 = 2 * m * (m + n);
            if (L0 > LMAX) break;

            for (int L = L0; L <= LMAX; L += L0) {
                cnt[L]++;
            }
        }
    }

    int answer = 0;
    for (int L = 1; L <= LMAX; L++) {
        if (cnt[L] == 1) answer++;
    }

    cout << answer << endl;
    return 0;
}