Perfect Square Collection
Find the smallest x + y + z with x > y > z > 0 such that all six quantities x+y, x-y, x+z, x-z, y+z, y-z are perfect squares.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Find the smallest \(x + y + z\) with integers \(x \gt y \gt z \gt 0\) such that \(x + y\), \(x - y\), \(x + z\), \(x - z\), \(y + z\), \(y - z\) are all perfect squares.
Problem 142: Perfect Square Collection
Mathematical Development
Theorem 1. Let , , , , , . Then the following Pythagorean-type identities hold:
Proof. Each identity follows from subtraction of the original definitions:
- , so .
- , so .
- , so .
- , so .
Theorem 2. The variables are recovered as:
For to be positive integers, , , and .
Proof. From the definitions: and , so and . For these to be integers, , i.e., . Positivity of requires , and positivity of requires (equivalently ). The other recovery formulas follow similarly.
Lemma 1. Identity (4) is redundant: it follows from identities (1)—(3).
Proof. From (1): . From (2): . From (3): . Then:
which is identity (4).
Lemma 2. For the ordering , we need , , and .
Proof. . . . The ordering among follows from and .
Editorial
The identities in the mathematical development suggest treating as the outer quantity and searching for compatible decompositions underneath it. Once is fixed, every valid solution must first split as
so the program enumerates those Pythagorean decompositions by trying each possible and checking whether the remaining value is a square.
For each surviving pair , the next task is to choose so that
Those two square tests are enough to reconstruct and , and the remaining identity is verified explicitly before building . At that point the formulas
recover the candidate triple, and the search keeps the smallest value of that satisfies the strict ordering .
Pseudocode
Start with the best sum marked as infinity.
For each $a$ in a generous search range:
Compute $a^2$.
For each positive $f<a$:
Let $c^2=a^2-f^2$.
Continue only when $c^2$ is a perfect square.
For each $e$ with $1 \le e < c$:
Let $b^2=c^2-e^2$ and require it to be a perfect square.
Let $d^2=a^2-e^2$ and require it to be a perfect square as well.
Check that $b^2+f^2=d^2$ really holds.
Reconstruct
$$
x=\frac{a^2+b^2}{2},\qquad y=\frac{a^2-b^2}{2},\qquad z=e^2-y.
$$
If these are integers with $x>y>z>0$, compute $x+y+z$.
Replace the current best answer whenever this sum is smaller.
Return the smallest sum found.
Complexity Analysis
- Time: where is the value of at termination. The number of representations of as a sum of two squares is (divisor-bounded). For each , we check pairs of decompositions.
- Space: per value of for storing decompositions.
In practice, the search terminates at small (the answer has ), making runtime negligible.
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() {
// Problem 142: Perfect Square Collection
// Find smallest x+y+z with x>y>z>0, all six sums/differences are perfect squares.
//
// Let a^2=x+y, b^2=x-y, c^2=x+z, d^2=x-z, e^2=y+z, f^2=y-z.
// Relations: a^2 = c^2 + f^2, c^2 = b^2 + e^2, a^2 = d^2 + e^2 (when d<e or d>e)
// Strategy: for each a, decompose a^2 = c^2 + f^2, then for each e < c,
// check if c^2 - e^2 = b^2, and a^2 - e^2 = d^2.
long long best = LLONG_MAX;
for (long long a = 3; a <= 1000; a++) {
long long a2 = a * a;
for (long long f = 1; f < a; f++) {
long long c2 = a2 - f * f;
if (c2 <= 0) break;
long long c = (long long)sqrt((double)c2);
if (c * c != c2) continue;
if (c <= 0) continue;
for (long long e = 1; e < c; e++) {
long long b2 = c * c - e * e;
if (b2 <= 0) continue;
long long b = (long long)sqrt((double)b2);
if (b * b != b2) continue;
long long d2 = a2 - e * e;
if (d2 <= 0) continue;
long long d = (long long)sqrt((double)d2);
if (d * d != d2) continue;
// Verify b^2 + f^2 = d^2
if (b2 + f * f != d2) continue;
// Parity check
if ((a2 + b2) % 2 != 0) continue;
long long x = (a2 + b2) / 2;
long long y = (a2 - b2) / 2;
long long z = e * e - y;
if (x > y && y > z && z > 0) {
long long s = x + y + z;
if (s < best) best = s;
}
}
}
}
cout << best << endl;
return 0;
}
"""
Problem 142: Perfect Square Collection
Find the smallest x+y+z with x>y>z>0 such that
x+y, x-y, x+z, x-z, y+z, y-z are all perfect squares.
Let a^2=x+y, b^2=x-y, c^2=x+z, d^2=x-z, e^2=y+z, f^2=y-z.
Key relations:
a^2 - f^2 = c^2 => a^2 = c^2 + f^2
c^2 - b^2 = e^2 => c^2 = b^2 + e^2
(and then d^2 = a^2 - e^2, verify b^2 + f^2 = d^2)
Strategy: for each a, find all ways to write a^2 = c^2 + f^2.
For each such (c, f), check if c^2 - b^2 = e^2 for some b, e
with a^2 - e^2 = d^2 (perfect square) and b^2 + f^2 = d^2.
Simpler: for each a, find all (c, f) with a^2 = c^2 + f^2.
Then for each (c, f), iterate over e < c, check if c^2 - e^2 = b^2
(perfect square), and then check if a^2 - e^2 = d^2 (perfect square).
"""
from math import isqrt
def solve():
best = float('inf')
best_xyz = None
for a in range(3, 1001):
a2 = a * a
# Find all (c, f) with a^2 = c^2 + f^2, c > 0, f > 0
for f in range(1, a):
c2 = a2 - f * f
if c2 <= 0:
break
c = isqrt(c2)
if c * c != c2:
continue
if c <= 0:
continue
# Now find e such that c^2 - e^2 = b^2 (perfect square)
# e must satisfy: e > 0, c^2 - e^2 > 0 => e < c
# Also: a^2 - e^2 must be a perfect square d^2
# And: b^2 + f^2 = d^2
for e in range(1, c):
b2 = c * c - e * e
if b2 <= 0:
continue
b = isqrt(b2)
if b * b != b2:
continue
# Check a^2 - e^2 = d^2
d2 = a2 - e * e
if d2 <= 0:
continue
d = isqrt(d2)
if d * d != d2:
continue
# Verify b^2 + f^2 = d^2
if b2 + f * f != d2:
continue
# Compute x, y, z
# x = (a^2 + b^2) / 2, y = (a^2 - b^2) / 2
if (a2 + b2) % 2 != 0:
continue
x = (a2 + b2) // 2
y = (a2 - b2) // 2
# z from e^2 = y+z => z = e^2 - y
z = e * e - y
if x > y > z > 0:
s = x + y + z
if s < best:
best = s
best_xyz = (x, y, z)
print(best)
if best_xyz:
x, y, z = best_xyz
print(f"x={x}, y={y}, z={z}")
vals = {'x+y': x+y, 'x-y': x-y, 'x+z': x+z, 'x-z': x-z, 'y+z': y+z, 'y-z': y-z}
for name, val in vals.items():
s = isqrt(val)
print(f" {name} = {val} = {s}^2 (check: {s*s == val})")
if __name__ == '__main__':
solve()