Integer Right Triangles
For which value of p <= 1000 is the number of integer-sided right triangles with perimeter p maximized?
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 is a triple of positive integers satisfying . It is primitive if .
Definition 2. For a positive integer , define
Theoretical Development
Theorem 1 (Euclid’s parametrization). Every primitive Pythagorean triple with odd and even is uniquely given by
where satisfy , , and . Conversely, every such produces a primitive triple.
Proof. This is classical. From with and standard divisibility arguments, one establishes the bijection. The coprimality and opposite-parity conditions on are necessary and sufficient for primitivity.
Lemma 1 (Perimeter formula). For the primitive triple generated by , the perimeter is . The general triple for has perimeter .
Proof. . Scaling by multiplies the perimeter by .
Corollary 1.1. Every Pythagorean triple has even perimeter. Hence for all odd .
Proof. By Lemma 1, is even.
Theorem 2 (Closed-form for ). Given and with , we have
Proof. Substitute into :
Cancelling :
Lemma 2 (Bounds on ). For a valid triple with and :
- .
- .
Proof. From and : since and , we get , whence . Also requires , i.e., , but is the tighter bound.
Theorem 3 (Counting via divisors). equals the number of integers with such that and the resulting .
Proof. By Theorem 2, given and , the value is the unique candidate. It yields a valid triple iff is a positive integer and . The bound is from Lemma 2.
Theorem 4 (Solution). Among all , the maximum of is achieved at , with .
Proof. has divisors, making it highly composite. By Theorem 3, each valid factorization contributes one triple. The large divisor count of 840 maximizes the number of such factorizations.
The 8 triples with perimeter 840 are:
| Verification | |||
|---|---|---|---|
| 40 | 399 | 401 | |
| 56 | 390 | 394 | |
| 105 | 360 | 375 | |
| 120 | 350 | 370 | |
| 140 | 336 | 364 | |
| 168 | 315 | 357 | |
| 210 | 280 | 350 | |
| 240 | 252 | 348 |
Exhaustive computation over all even confirms no other perimeter achieves .
Editorial
We examine only even perimeters, since odd perimeters cannot occur for Pythagorean triples. For each admissible perimeter , we enumerate the smallest side in its valid range, recover the unique candidate value of from the derived closed formula, and count the cases in which is an integer satisfying . 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 time and space.
Proof. The outer loop runs times (even values only). The inner loop runs at most times per iteration. Each iteration performs arithmetic (a division and modulus check). Total operations: . Hence . With : at most iterations.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""Project Euler Problem 39: Integer Right Triangles"""
def solve():
LIMIT = 1000
best_p, best_count = 0, 0
for p in range(2, LIMIT + 1, 2):
count = 0
for a in range(1, p // 3):
num = p * (p - 2 * a)
den = 2 * (p - a)
if num % den == 0 and num // den >= a:
count += 1
if count > best_count:
best_count = count
best_p = p
return best_p
answer = solve()
assert answer == 840
print(answer)