All Euler problems
Project Euler

Perfect Right-angled Triangles

A right-angled triangle with sides a, b, and hypotenuse c is called perfect if: 1. (a, b, c) is a primitive Pythagorean triple. 2. The hypotenuse c is a perfect square. How many perfect right-angle...

Source sync May 21, 2026
Problem #0218
Level Level 08
Solved By 3,427
Languages C++, Python
Answer 0
Length 291 words
geometrynumber_theorymodular_arithmetic

Problem Statement

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

Consider the right angled triangle with sides \(a=7\), \(b=24\) and \(c=25\). The area of this triangle is \(84\), which is divisible by the perfect numbers \(6\) and \(28\).

Moreover it is a primitive right angled triangle as \(\gcd (a,b)=1\) and \(\gcd (b,c)=1\).

Also \(c\) is a perfect square.

We will call a right angled triangle perfect if

  • it is a primitive right angled triangle

  • its hypotenuse is a perfect square.

We will call a right angled triangle super-perfect if

  • it is a perfect right angled triangle and

  • its area is a multiple of the perfect numbers \(6\) and \(28\).

How many perfect right-angled triangles with \(c \le 10^{16}\) exist that are not super-perfect?

Problem 218: Perfect Right-angled Triangles

Mathematical Development

Theorem 1 (Parametrization of primitive Pythagorean triples). Every primitive Pythagorean triple (a,b,c)(a,b,c) with bb even is given by

a=m2−n2,b=2mn,c=m2+n2,a = m^2 - n^2, \qquad b = 2mn, \qquad 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.

Proof. This is the classical Euclid parametrization. □\square

Lemma 1 (Area formula). The area is

A=mn(m−n)(m+n).A = mn(m-n)(m+n).

Proof. Since A=ab/2A = ab/2,

A=(m2−n2)(2mn)2=mn(m−n)(m+n).□A = \frac{(m^2-n^2)(2mn)}{2} = mn(m-n)(m+n). \qquad \square

Theorem 2 (Every perfect triangle has area divisible by 84). For every perfect right-angled triangle,

84∣A.84 \mid A.

In particular, its area is divisible by both 6 and 28.

Proof. Since the hypotenuse is a perfect square, say

c=m2+n2=k2,c = m^2 + n^2 = k^2,

the triple (m,n,k)(m,n,k) is itself a primitive Pythagorean triple. Hence one may write

m=u2−v2,n=2uvm = u^2 - v^2, \qquad n = 2uv

or the symmetric variant.

  • Factor 3: if neither mm nor nn were divisible by 3, then m2+n2≡1+1≡2(mod3),m^2 + n^2 \equiv 1 + 1 \equiv 2 \pmod 3, impossible for a square. So 3∣mn3 \mid mn.
  • Factor 4: the even parameter is 2uv2uv, and one of u,vu,v is even, so in fact 44 divides that factor.
  • Factor 7: substituting the second parametrization and checking residues modulo 7 shows that one of m, n, m−n, m+nm,\ n,\ m-n,\ m+n is always divisible by 7.

Therefore 3⋅4⋅7=843 \cdot 4 \cdot 7 = 84 divides

mn(m−n)(m+n)=A.□mn(m-n)(m+n) = A. \qquad \square

Editorial

There is no search left once the second parametrization is used. A perfect triangle already starts as a primitive Pythagorean triple, and the extra condition that the hypotenuse is a square forces the parameters (m,n)(m,n) to form another primitive Pythagorean triple. That second layer is what injects the extra divisibility.

From the resulting area formula

A=mn(m−n)(m+n),A = mn(m-n)(m+n),

the factors 3 and 4 are immediate, and a short residue check supplies the factor 7. So every perfect triangle has area divisible by 84, which means no triangle can fail both the divisibility-by-6 and divisibility-by-28 requirements.

Pseudocode

Use the primitive Pythagorean parametrization for (a, b, c).
Use the condition c = square to parametrize (m, n, k) as another primitive triple.
Deduce from the area formula that every candidate area has factors 3, 4, and 7.
Since every perfect triangle has area divisible by 84, the number of exceptions is 0.
Return 0.

Complexity Analysis

  • Time: O(1)O(1).
  • Space: O(1)O(1).

Answer

0\boxed{0}

Code

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

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

long long countNonSuperPerfect(long long /*limit*/) {
    // The proof shows every perfect right-angled triangle has area divisible by 84,
    // so none can fail both the divisibility-by-6 and divisibility-by-28 tests.
    return 0;
}

int main() {
    cout << countNonSuperPerfect(10000000000000000LL) << '\n';
    return 0;
}