All Euler problems
Project Euler

Integer Right Triangles

For which value of p <= 1000 is the number of integer-sided right triangles with perimeter p maximized?

Source sync Apr 19, 2026
Problem #0039
Level Level 01
Solved By 80,963
Languages C++, Python
Answer 840
Length 464 words
geometrynumber_theorymodular_arithmetic

Problem Statement

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

If \(p\) is the perimeter of a right angle triangle with integral length sides, \(\{a, b, c\}\), there are exactly three solutions for \(p = 120\).

\(\{20,48,52\}\), \(\{24,45,51\}\), \(\{30,40,50\}\)

For which value of \(p \le 1000\), is the number of solutions maximised?

Problem 39: Integer Right Triangles

Mathematical Development

Definitions

Definition 1. A Pythagorean triple (a,b,c)(a, b, c) is a triple of positive integers satisfying a2+b2=c2a^2 + b^2 = c^2. It is primitive if gcd(a,b,c)=1\gcd(a, b, c) = 1.

Definition 2. For a positive integer pp, define

N(p)=#{(a,b,c)Z>03:ab<c,  a2+b2=c2,  a+b+c=p}.N(p) = \#\{(a, b, c) \in \mathbb{Z}_{>0}^3 : a \le b < c,\; a^2 + b^2 = c^2,\; a + b + c = p\}.

Theoretical Development

Theorem 1 (Euclid’s parametrization). Every primitive Pythagorean triple (a,b,c)(a, b, c) with aa odd and bb even is uniquely given by

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

where m,nZ>0m, n \in \mathbb{Z}_{>0} satisfy m>nm > n, gcd(m,n)=1\gcd(m, n) = 1, and m≢n(mod2)m \not\equiv n \pmod{2}. Conversely, every such (m,n)(m, n) produces a primitive triple.

Proof. This is classical. From a2=c2b2=(cb)(c+b)a^2 = c^2 - b^2 = (c-b)(c+b) with gcd(a,b,c)=1\gcd(a,b,c) = 1 and standard divisibility arguments, one establishes the bijection. The coprimality and opposite-parity conditions on (m,n)(m, n) are necessary and sufficient for primitivity. \blacksquare

Lemma 1 (Perimeter formula). For the primitive triple generated by (m,n)(m, n), the perimeter is p0=2m(m+n)p_0 = 2m(m + n). The general triple (ka,kb,kc)(ka, kb, kc) for k1k \ge 1 has perimeter p=2km(m+n)p = 2km(m + n).

Proof. a+b+c=(m2n2)+2mn+(m2+n2)=2m2+2mn=2m(m+n)a + b + c = (m^2 - n^2) + 2mn + (m^2 + n^2) = 2m^2 + 2mn = 2m(m + n). Scaling by kk multiplies the perimeter by kk. \blacksquare

Corollary 1.1. Every Pythagorean triple has even perimeter. Hence N(p)=0N(p) = 0 for all odd pp.

Proof. By Lemma 1, p=2km(m+n)p = 2km(m+n) is even. \blacksquare

Theorem 2 (Closed-form for bb). Given a+b+c=pa + b + c = p and a2+b2=c2a^2 + b^2 = c^2 with a,b,c>0a, b, c > 0, we have

b=p(p2a)2(pa).b = \frac{p(p - 2a)}{2(p - a)}.

Proof. Substitute c=pabc = p - a - b into a2+b2=c2a^2 + b^2 = c^2:

a2+b2=(pab)2=p22p(a+b)+(a+b)2=p22pa2pb+a2+2ab+b2.\begin{align*} a^2 + b^2 &= (p - a - b)^2 = p^2 - 2p(a+b) + (a+b)^2 \\ &= p^2 - 2pa - 2pb + a^2 + 2ab + b^2. \end{align*}

Cancelling a2+b2a^2 + b^2:

0=p22pa2pb+2ab,2b(pa)=p(p2a),b=p(p2a)2(pa).0 = p^2 - 2pa - 2pb + 2ab, \quad 2b(p - a) = p(p - 2a), \quad b = \frac{p(p-2a)}{2(p-a)}.

\blacksquare

Lemma 2 (Bounds on aa). For a valid triple with 1ab<c1 \le a \le b < c and a+b+c=pa + b + c = p:

  1. a1a \ge 1.
  2. a<p/3a < p/3.

Proof. From ab<ca \le b < c and a+b+c=pa + b + c = p: since bab \ge a and c>bac > b \ge a, we get p=a+b+c>3ap = a + b + c > 3a, whence a<p/3a < p/3. Also b>0b > 0 requires p(p2a)>0p(p - 2a) > 0, i.e., a<p/2a < p/2, but p/3<p/2p/3 < p/2 is the tighter bound. \blacksquare

Theorem 3 (Counting via divisors). N(p)N(p) equals the number of integers aa with 1a<p/31 \le a < p/3 such that 2(pa)p(p2a)2(p-a) \mid p(p-2a) and the resulting bab \ge a.

Proof. By Theorem 2, given pp and aa, the value b=p(p2a)/[2(pa)]b = p(p-2a)/[2(p-a)] is the unique candidate. It yields a valid triple iff bb is a positive integer and bab \ge a. The bound a<p/3a < p/3 is from Lemma 2. \blacksquare

Theorem 4 (Solution). Among all p1000p \le 1000, the maximum of N(p)N(p) is achieved at p=840p = 840, with N(840)=8N(840) = 8.

Proof. 840=23357840 = 2^3 \cdot 3 \cdot 5 \cdot 7 has τ(840)=32\tau(840) = 32 divisors, making it highly composite. By Theorem 3, each valid factorization p=2km(m+n)p = 2km(m+n) contributes one triple. The large divisor count of 840 maximizes the number of such factorizations.

The 8 triples with perimeter 840 are:

aabbccVerification
40399401402+3992=1600+159201=160801=401240^2 + 399^2 = 1600 + 159201 = 160801 = 401^2
56390394562+3902=3136+152100=155236=394256^2 + 390^2 = 3136 + 152100 = 155236 = 394^2
1053603751052+3602=11025+129600=140625=3752105^2 + 360^2 = 11025 + 129600 = 140625 = 375^2
1203503701202+3502=14400+122500=136900=3702120^2 + 350^2 = 14400 + 122500 = 136900 = 370^2
1403363641402+3362=19600+112896=132496=3642140^2 + 336^2 = 19600 + 112896 = 132496 = 364^2
1683153571682+3152=28224+99225=127449=3572168^2 + 315^2 = 28224 + 99225 = 127449 = 357^2
2102803502102+2802=44100+78400=122500=3502210^2 + 280^2 = 44100 + 78400 = 122500 = 350^2
2402523482402+2522=57600+63504=121104=3482240^2 + 252^2 = 57600 + 63504 = 121104 = 348^2

Exhaustive computation over all even p1000p \le 1000 confirms no other perimeter achieves N(p)8N(p) \ge 8. \blacksquare

Editorial

We examine only even perimeters, since odd perimeters cannot occur for Pythagorean triples. For each admissible perimeter pPp \le P, we enumerate the smallest side aa in its valid range, recover the unique candidate value of bb from the derived closed formula, and count the cases in which bb is an integer satisfying bab \ge a. The perimeter with the largest count is retained throughout the scan.

Pseudocode

Algorithm: Perimeter with the Most Integer Right Triangles
Require: A perimeter bound P.
Ensure: The perimeter p ≤ P for which the equation a^2 + b^2 = c^2 with a + b + c = p has the most integer solutions.
1: Initialize best_p ← 0 and best_count ← 0.
2: For each even perimeter p with 12 ≤ p ≤ P do:
3:     Count the admissible values of a for which b ← p(p - 2a) / (2(p - a)) is integral and yields a valid Pythagorean triple.
4:     If this count exceeds best_count, update best_count ← count and best_p ← p.
5: Return best_p.

Complexity Analysis

Proposition. The algorithm runs in O(P2)O(P^2) time and O(1)O(1) space.

Proof. The outer loop runs P/2P/2 times (even values only). The inner loop runs at most p/3<P/3\lfloor p/3 \rfloor < P/3 times per iteration. Each iteration performs O(1)O(1) arithmetic (a division and modulus check). Total operations: p evenp/3(P/2)(P/3)=P2/6\sum_{p \text{ even}} \lfloor p/3 \rfloor \le (P/2) \cdot (P/3) = P^2/6. Hence O(P2)O(P^2). With P=1000P = 1000: at most 1.7×105\sim 1.7 \times 10^5 iterations. \blacksquare

Answer

840\boxed{840}

Code

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

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

int main() {
    const int LIMIT = 1000;
    int best_p = 0, best_count = 0;

    for (int p = 2; p <= LIMIT; p += 2) {
        int count = 0;
        for (int a = 1; a < p / 3; a++) {
            int num = p * (p - 2 * a);
            int den = 2 * (p - a);
            if (num % den == 0 && num / den >= a)
                count++;
        }
        if (count > best_count) {
            best_count = count;
            best_p = p;
        }
    }

    cout << best_p << endl;
    return 0;
}