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...
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.

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 where , , and .
Proof. Classical result. See any number theory textbook (e.g., Hardy & Wright, Chapter 13).
Theorem 2. The tiling condition is preserved under scaling: if it holds for a primitive triple , it holds for for any positive integer .
Proof. .
Lemma 1. For the primitive triple :
The tiling condition becomes .
Proof. Direct computation: . So . The tiling condition is , i.e., .
Theorem 3. For each primitive triple satisfying the tiling condition with perimeter , the number of valid triples (including non-primitive) with perimeter at most is .
Proof. By Theorem 2, every multiple also satisfies the tiling condition, and its perimeter is . We need , giving . 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).
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: — the number of primitive Pythagorean triples with perimeter is by a result of Lehmer. For each, we do work for the GCD.
- Space: auxiliary space (no sieve needed; GCD is computed on the fly).
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 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;
}
"""
Problem 139: Pythagorean Tiles
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.
"""
import math
def solve():
P = 100_000_000
count = 0
m = 2
while 2 * m * (m + 1) <= P:
for n in range(1, m):
if (m - n) % 2 == 0:
continue
if math.gcd(m, n) != 1:
continue
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
perim = a + b + c
if perim > P:
break
diff = abs(b - a)
if diff > 0 and c % diff == 0:
count += P // perim
m += 1
return count
if __name__ == "__main__":
answer = solve()
assert answer == 10057761
print(answer)