All Euler problems
Project Euler

Pythagorean Tiles

Given a right triangle with integer sides (a, b, c) (a Pythagorean triple, a^2 + b^2 = c^2) and perimeter at most 100,000,000, how many such triples allow the square on the hypotenuse to be perfect...

Source sync Apr 19, 2026
Problem #0139
Level Level 06
Solved By 6,573
Languages C++, Python
Answer 10057761
Length 266 words
geometrymodular_arithmeticnumber_theory

Problem Statement

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

Let \((a, b, c)\) represent the three sides of a right angle triangle with integral length sides. It is possible to place four such triangles together to form a square with length \(c\).

For example, \((3, 4, 5)\) triangles can be placed together to form a \(5\) by \(5\) square with a \(1\) by \(1\) hole in the middle and it can be seen that the \(5\) by \(5\) square can be tiled with twenty-five \(1\) by \(1\) squares.

PIC

However, if \((5, 12, 13)\) triangles were used then the hole would measure \(7\) by \(7\) and these could not be used to tile the \(13\) by \(13\) square.

Given that the perimeter of the right triangle is less than one-hundred million, how many Pythagorean triangles would allow such a tiling to take place?

Problem 139: Pythagorean Tiles

Mathematical Foundation

Theorem 1. Every primitive Pythagorean triple can be parameterized as (a,b,c)=(m2n2,  2mn,  m2+n2)(a, b, c) = (m^2 - n^2, \; 2mn, \; 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. Classical result. See any number theory textbook (e.g., Hardy & Wright, Chapter 13). \square

Theorem 2. The tiling condition bac|b - a| \mid c is preserved under scaling: if it holds for a primitive triple (a,b,c)(a, b, c), it holds for (ka,kb,kc)(ka, kb, kc) for any positive integer kk.

Proof. kbkakc    kbakc    bac|kb - ka| \mid kc \iff k|b - a| \mid kc \iff |b - a| \mid c. \square

Lemma 1. For the primitive triple (a,b,c)=(m2n2,2mn,m2+n2)(a, b, c) = (m^2 - n^2, 2mn, m^2 + n^2):

ba=2mnm2+n2=m22mnn2(1)? or equivalently (mn)22n2|b - a| = |2mn - m^2 + n^2| = |m^2 - 2mn - n^2| \cdot (-1)^? \text{ or equivalently } |(m-n)^2 - 2n^2|

The tiling condition becomes (m2+n2)mod2mn(m2n2)=0(m^2 + n^2) \bmod |2mn - (m^2 - n^2)| = 0.

Proof. Direct computation: ba=2mn(m2n2)=(m22mnn2)=((mn)22n2)b - a = 2mn - (m^2 - n^2) = -(m^2 - 2mn - n^2) = -((m-n)^2 - 2n^2). So ba=(mn)22n2|b - a| = |(m-n)^2 - 2n^2|. The tiling condition is bac|b - a| \mid c, i.e., (m2+n2)modba=0(m^2 + n^2) \bmod |b - a| = 0. \square

Theorem 3. For each primitive triple satisfying the tiling condition with perimeter p0=2m(m+n)p_0 = 2m(m+n), the number of valid triples (including non-primitive) with perimeter at most PP is P/p0\lfloor P / p_0 \rfloor.

Proof. By Theorem 2, every multiple k(a,b,c)k(a, b, c) also satisfies the tiling condition, and its perimeter is kp0kp_0. We need kp0Pkp_0 \leq P, giving kP/p0k \leq \lfloor P/p_0 \rfloor. Every non-primitive Pythagorean triple satisfying the condition is a multiple of some primitive triple satisfying the condition (since the condition is scale-invariant by Theorem 2 and every Pythagorean triple is a multiple of a primitive one). \square

Editorial

Count Pythagorean triples (a, b, c) with perimeter <= 10^8 where c % |b - a| == 0 (the tiling condition). Enumerate primitive triples via (m, n) parameterization and count multiples.

Pseudocode

    P = 100000000
    count = 0
    For m from 2 to floor(sqrt(P/2)):
        For n from 1 to m-1:
            if (m - n) % 2 == 0: // same parity, skip
                continue
            If gcd(m, n) != 1 then
                continue
            a = m*m - n*n
            b = 2*m*n
            c = m*m + n*n
            perimeter = a + b + c // = 2*m*(m+n)
            If perimeter > P then
                break
            diff = abs(b - a)
            If c % diff == 0 then
                count += P // perimeter // floor division
    Return count

Complexity Analysis

  • Time: O(P/logP)O(P / \log P) — the number of primitive Pythagorean triples with perimeter P\leq P is O(P/logP)O(P / \log P) by a result of Lehmer. For each, we do O(logm)O(\log m) work for the GCD.
  • Space: O(1)O(1) auxiliary space (no sieve needed; GCD is computed on the fly).

Answer

10057761\boxed{10057761}

Code

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

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

int main() {
    const long long P = 100000000LL;
    long long count = 0;

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

            long long a = m * m - n * n;
            long long b = 2 * m * n;
            long long c = m * m + n * n;
            long long perim = a + b + c; // = 2*m*(m+n)

            if (perim > P) break;

            long long diff = abs(b - a);
            if (diff > 0 && c % diff == 0) {
                count += P / perim;
            }
        }
    }

    cout << count << endl;
    return 0;
}