All Euler problems
Project Euler

Special Pythagorean Triplet

A Pythagorean triplet is a tuple of positive integers (a, b, c) with a < b < c and a^2 + b^2 = c^2. Find the unique such triplet satisfying a + b + c = 1000, and compute the product abc.

Source sync Apr 19, 2026
Problem #0009
Level Level 00
Solved By 384,372
Languages C++, Python
Answer 31875000
Length 300 words
modular_arithmeticnumber_theoryalgebra

Problem Statement

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

A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which, $$a^2 + b^2 = c^2.$$ For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$.

There exists exactly one Pythagorean triplet for which $a + b + c = 1000$.

Find the product $abc$.

Problem 9: Special Pythagorean Triplet

Mathematical Development

Parametric Reduction

Theorem 1 (Reduction to a single variable). Suppose positive integers a,b,ca, b, c satisfy

a2+b2=c2anda+b+c=sa^2 + b^2 = c^2 \quad \text{and} \quad a + b + c = s

for a given s>0s > 0. Then

b=s(s2a)2(sa).b = \frac{s(s - 2a)}{2(s - a)}.

Proof. From the sum constraint, c=sabc = s - a - b. Substituting into the Pythagorean condition:

a2+b2=(sab)2=s22s(a+b)+(a+b)2=s22sa2sb+a2+2ab+b2.a^2 + b^2 = (s - a - b)^2 = s^2 - 2s(a + b) + (a + b)^2 = s^2 - 2sa - 2sb + a^2 + 2ab + b^2.

Cancelling a2+b2a^2 + b^2 from both sides:

0=s22sa2sb+2ab.0 = s^2 - 2sa - 2sb + 2ab.

Solving for bb:

2sb2ab=s22sa    b(2s2a)=s22sa=s(s2a),2sb - 2ab = s^2 - 2sa \implies b(2s - 2a) = s^2 - 2sa = s(s - 2a),

hence b=s(s2a)2(sa)b = \frac{s(s - 2a)}{2(s - a)}. \square

Corollary 1. Setting s=1000s = 1000:

b=1000(10002a)2(1000a)=500(10002a)1000a=5000001000a1000a.b = \frac{1000(1000 - 2a)}{2(1000 - a)} = \frac{500(1000 - 2a)}{1000 - a} = \frac{500000 - 1000a}{1000 - a}.

Theorem 2 (Integrality condition). With s=1000s = 1000, the value bb is a positive integer if and only if (1000a)500000(1000 - a) \mid 500000.

Proof. Perform polynomial long division on the numerator:

5000001000a=1000(a1000)+5000001000000=1000(a1000)500000.500000 - 1000a = -1000(a - 1000) + 500000 - 1000000 = -1000(a - 1000) - 500000.

Equivalently, write 5000001000a=1000(1000a)500000500000 - 1000a = 1000(1000 - a) - 500000, so

b=1000(1000a)5000001000a=10005000001000a.b = \frac{1000(1000 - a) - 500000}{1000 - a} = 1000 - \frac{500000}{1000 - a}.

For bZ+b \in \mathbb{Z}^+, we need (1000a)500000(1000 - a) \mid 500000 and 500000/(1000a)<1000500000/(1000 - a) < 1000, i.e., 1000a>5001000 - a > 500, i.e., a<500a < 500. \square

Enumeration of Solutions

Theorem 3 (Existence and uniqueness). The unique Pythagorean triplet (a,b,c)(a, b, c) with a<b<ca < b < c and a+b+c=1000a + b + c = 1000 is (200,375,425)(200, 375, 425).

Proof. We require:

  1. (1000a)500000(1000 - a) \mid 500000,
  2. a<b<ca < b < c with a,b,c>0a, b, c > 0.

The prime factorization 500000=2556500000 = 2^5 \cdot 5^6 has (5+1)(6+1)=42(5+1)(6+1) = 42 positive divisors. Setting d=1000ad = 1000 - a, we need d500000d \mid 500000 and a=1000d>0a = 1000 - d > 0, so d<1000d < 1000. Also d>500d > 500 by Theorem 2.

The divisors of 500000500000 in the range (500,1000)(500, 1000) are:

  • d=625d = 625: a=375a = 375, b=1000500000/625=1000800=200b = 1000 - 500000/625 = 1000 - 800 = 200, c=425c = 425. Here b<ab < a, so reorder: (a,b,c)=(200,375,425)(a, b, c) = (200, 375, 425).
  • d=800d = 800: a=200a = 200, b=1000500000/800=1000625=375b = 1000 - 500000/800 = 1000 - 625 = 375, c=425c = 425. This gives (200,375,425)(200, 375, 425).

Both cases yield the same triplet. No other divisor of 500000500000 lies in (500,1000)(500, 1000).

Verification. 2002+3752=40000+140625=180625=4252200^2 + 375^2 = 40000 + 140625 = 180625 = 425^2. Also 200+375+425=1000200 + 375 + 425 = 1000. The ordering 200<375<425200 < 375 < 425 holds. \square

Remark. The triplet (200,375,425)=25(8,15,17)(200, 375, 425) = 25 \cdot (8, 15, 17), where (8,15,17)(8, 15, 17) is a primitive Pythagorean triple generated by the classical parametrization with m=4,n=1m = 4, n = 1: a=2mn=8a = 2mn = 8, b=m2n2=15b = m^2 - n^2 = 15, c=m2+n2=17c = m^2 + n^2 = 17.

Editorial

We iterate over the possible first leg aa and use the linear relation derived from a+b+c=sa+b+c=s and a2+b2=c2a^2+b^2=c^2 to solve for bb. For each aa, we compute the candidate numerator and denominator, check whether the division is exact, recover bb and c=sabc = s-a-b, and return the product once the ordering a<b<ca < b < c holds. This is sufficient because every Pythagorean triple with sum ss must satisfy the derived formula for bb.

Pseudocode

Algorithm: Special Pythagorean Triple for a Fixed Perimeter
Require: A target perimeter s.
Ensure: The product abc for the unique Pythagorean triple with a + b + c = s.
1: For each admissible value of a in increasing order do:
2:     Compute nu ← s(s - 2a) and delta ← 2(s - a).
3:     If delta divides nu, set b ← nu / delta and c ← s - a - b.
4:     If a < b < c and a^2 + b^2 = c^2, return a · b · c.
5: Return failure if no such triple exists.

Complexity Analysis

Theorem 4. The algorithm finds the triplet in O(s)O(s) time and O(1)O(1) space, where s=a+b+cs = a + b + c.

Proof. The outer loop iterates aa from 1 to at most s/3s/3 (since a<b<ca < b < c and a+b+c=sa + b + c = s imply a<s/3a < s/3). Each iteration performs O(1)O(1) arithmetic: one division and a divisibility check. No auxiliary data structures are needed. \square

Remark. An O(s)O(\sqrt{s}) algorithm is possible by directly iterating over divisors of s2/4s^2/4, but the linear approach suffices for s=1000s = 1000.

Answer

31875000\boxed{31875000}

Code

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

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

int main() {
    const int s = 1000;
    for (int a = 1; a < s / 3; a++) {
        int num = s * s / 2 - s * a;
        int den = s - a;
        if (num % den == 0) {
            int b = num / den;
            int c = s - a - b;
            if (a < b && b < c) {
                cout << (long long)a * b * c << endl;
                return 0;
            }
        }
    }
    return 0;
}