All Euler problems
Project Euler

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.

Source sync Apr 19, 2026
Problem #0142
Level Level 05
Solved By 6,693
Languages C++, Python
Answer 1006193
Length 315 words
game_theorymodular_arithmeticbrute_force

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 Foundation

Theorem 1. Let a2=x+ya^2 = x+y, b2=xyb^2 = x-y, c2=x+zc^2 = x+z, d2=xzd^2 = x-z, e2=y+ze^2 = y+z, f2=yzf^2 = y-z. Then the following Pythagorean-type identities hold:

  1. a2=c2+f2a^2 = c^2 + f^2
  2. a2=d2+e2a^2 = d^2 + e^2
  3. c2=b2+e2c^2 = b^2 + e^2
  4. d2=b2+f2d^2 = b^2 + f^2

Proof. Each identity follows from subtraction of the original definitions:

  1. a2c2=(x+y)(x+z)=yz=f2a^2 - c^2 = (x+y) - (x+z) = y - z = f^2, so a2=c2+f2a^2 = c^2 + f^2.
  2. a2e2=(x+y)(y+z)=xz=d2a^2 - e^2 = (x+y) - (y+z) = x - z = d^2, so a2=d2+e2a^2 = d^2 + e^2.
  3. c2e2=(x+z)(y+z)=xy=b2c^2 - e^2 = (x+z) - (y+z) = x - y = b^2, so c2=b2+e2c^2 = b^2 + e^2.
  4. d2b2=(xz)(xy)=yz=f2d^2 - b^2 = (x-z) - (x-y) = y - z = f^2, so d2=b2+f2d^2 = b^2 + f^2. \square

Theorem 2. The variables x,y,zx, y, z are recovered as:

x=a2+b22,y=a2b22,z=c2d22=e2f22x = \frac{a^2 + b^2}{2}, \quad y = \frac{a^2 - b^2}{2}, \quad z = \frac{c^2 - d^2}{2} = \frac{e^2 - f^2}{2}

For x,y,zx, y, z to be positive integers, ab(mod2)a \equiv b \pmod{2}, cd(mod2)c \equiv d \pmod{2}, and ef(mod2)e \equiv f \pmod{2}.

Proof. From the definitions: x+y=a2x + y = a^2 and xy=b2x - y = b^2, so x=(a2+b2)/2x = (a^2 + b^2)/2 and y=(a2b2)/2y = (a^2 - b^2)/2. For these to be integers, a2b2(mod2)a^2 \equiv b^2 \pmod{2}, i.e., ab(mod2)a \equiv b \pmod{2}. Positivity of yy requires a>ba > b, and positivity of zz requires c>dc > d (equivalently e>fe > f). The other recovery formulas follow similarly. \square

Lemma 1. Identity (4) is redundant: it follows from identities (1)—(3).

Proof. From (1): f2=a2c2f^2 = a^2 - c^2. From (2): d2=a2e2d^2 = a^2 - e^2. From (3): b2=c2e2b^2 = c^2 - e^2. Then:

d2b2=(a2e2)(c2e2)=a2c2=f2d^2 - b^2 = (a^2 - e^2) - (c^2 - e^2) = a^2 - c^2 = f^2

which is identity (4). \square

Lemma 2. For the ordering x>y>z>0x > y > z > 0, we need a>c>e>0a > c > e > 0, a>d>f>0a > d > f > 0, and b>0b > 0.

Proof. x>y    b2=xy>0    b>0x > y \iff b^2 = x - y > 0 \iff b > 0. y>z    f2=yz>0    f>0y > z \iff f^2 = y - z > 0 \iff f > 0. x>z    d2>0    d>0x > z \iff d^2 > 0 \iff d > 0. The ordering among a,c,ea, c, e follows from a2=c2+f2>c2a^2 = c^2 + f^2 > c^2 and c2=b2+e2>e2c^2 = b^2 + e^2 > e^2. \square

Editorial

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

Pseudocode

INPUT: Find minimum x + y + z
OUTPUT: Smallest valid x + y + z

Complexity Analysis

  • Time: O(A1+ϵ)O(A^{1+\epsilon}) where AA is the value of aa at termination. The number of representations of a2a^2 as a sum of two squares is O(aϵ)O(a^\epsilon) (divisor-bounded). For each aa, we check O(a2ϵ)O(a^{2\epsilon}) pairs of decompositions.
  • Space: O(Aϵ)O(A^\epsilon) per value of aa for storing decompositions.

In practice, the search terminates at small aa (the answer has a=468a = 468), making runtime negligible.

Answer

1006193\boxed{1006193}

Code

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

C++ project_euler/problem_142/solution.cpp
#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;
}