All Euler problems
Project Euler

Cuboid Route

A spider is located at one corner of a cuboid room with dimensions a x b x c, and a fly is at the diagonally opposite corner. By travelling on the surfaces of the room, find the shortest "straight...

Source sync May 21, 2026
Problem #0086
Level Level 04
Solved By 14,764
Languages C++, Python
Answer 1818
Length 489 words
brute_forcegeometryarithmetic

Problem Statement

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

A spider, S, sits in one corner of a cuboid room, measuring \(6\) by \(5\) by \(3\), and a fly, F, sits in the opposite corner. By travelling on the surfaces of the room the shortest "straight line" distance from S to F is \(10\) and the path is shown on the diagram.

PIC

However, there are up to three "shortest" path candidates for any given cuboid and the shortest route doesn’t always have integer length.

It can be shown that there are exactly \(2060\) distinct cuboids, ignoring rotations, with integer dimensions, up to a maximum size of \(M\) by \(M\) by \(M\), for which the shortest route has integer length when \(M = 100\). This is the least value of \(M\) for which the number of solutions first exceeds two thousand; the number of solutions when \(M = 99\) is \(1975\).

Find the least value of \(M\) such that the number of solutions first exceeds one million.

Problem 86: Cuboid Route

Mathematical Foundation

Shortest Surface Path via Unfolding

Theorem 1 (Shortest surface path). For a cuboid with ordered dimensions a≤b≤ca \le b \le c, the shortest surface path from one corner to the diagonally opposite corner has squared length

d2=(a+b)2+c2.d^2 = (a + b)^2 + c^2.

Proof. Unfolding the cuboid onto a plane produces three distinct planar configurations, each yielding a straight-line path connecting the two corners. The squared distances are:

d12=(a+b)2+c2,d22=(a+c)2+b2,d32=(b+c)2+a2.d_1^2 = (a + b)^2 + c^2, \qquad d_2^2 = (a + c)^2 + b^2, \qquad d_3^2 = (b + c)^2 + a^2.

We claim d12≤d22≤d32d_1^2 \le d_2^2 \le d_3^2 whenever a≤b≤ca \le b \le c.

Proof of d12≤d22d_1^2 \le d_2^2:

(a+b)2+c2≤(a+c)2+b2  ⟺  2ab+b2≤2ac+c2  ⟺  b(2a+b)≤c(2a+c).(a+b)^2 + c^2 \le (a+c)^2 + b^2 \iff 2ab + b^2 \le 2ac + c^2 \iff b(2a+b) \le c(2a+c).

Define g(x)=x(2a+x)=2ax+x2g(x) = x(2a + x) = 2ax + x^2. Since g′(x)=2a+2x>0g'(x) = 2a + 2x > 0 for x>0x > 0, gg is strictly increasing on (0,∞)(0, \infty). As b≤cb \le c, we have g(b)≤g(c)g(b) \le g(c).

Proof of d22≤d32d_2^2 \le d_3^2:

(a+c)2+b2≤(b+c)2+a2  ⟺  2ac+c2+b2≤2bc+c2+a2  ⟺  2c(a−b)≤a2−b2=(a−b)(a+b).(a+c)^2 + b^2 \le (b+c)^2 + a^2 \iff 2ac + c^2 + b^2 \le 2bc + c^2 + a^2 \iff 2c(a-b) \le a^2 - b^2 = (a-b)(a+b).

Since a≤ba \le b, we have a−b≤0a - b \le 0, so the left side is 2c(a−b)≤02c(a-b) \le 0 and the right side is (a−b)(a+b)≤0(a-b)(a+b) \le 0. Dividing by a−b<0a - b < 0 (when a<ba < b) reverses the inequality to 2c≥a+b2c \ge a + b, which holds since c≥b≥ac \ge b \ge a implies 2c≥a+b2c \ge a + b. When a=ba = b, both sides equal zero. □\square

Reduction to Pythagorean Triples

Lemma 1 (Integer path criterion). The shortest surface path has integer length if and only if there exists a positive integer dd such that (a+b)2+c2=d2(a+b)^2 + c^2 = d^2, i.e., (a+b,c,d)(a+b, c, d) forms a Pythagorean triple.

Proof. By Theorem 1, the squared path length is (a+b)2+c2(a+b)^2 + c^2. This is a perfect square if and only if the path length d=(a+b)2+c2d = \sqrt{(a+b)^2 + c^2} is a positive integer. □\square

Counting Valid Cuboids

Lemma 2 (Valid pair count). For a Pythagorean triple with legs ss and cc (where s=a+bs = a + b), the number of ordered pairs (a,b)(a, b) satisfying 1≤a≤b≤c1 \le a \le b \le c and a+b=sa + b = s is

N(s,c)=max⁡ ⁣(0,  ⌊s2⌋−max⁡(1, s−c)+1).N(s, c) = \max\!\left(0,\; \left\lfloor \frac{s}{2} \right\rfloor - \max(1,\, s - c) + 1\right).

Proof. We require simultaneously:

  • a+b=sa + b = s and a≤ba \le b, which gives a≤s/2a \le s/2, hence a≤⌊s/2⌋a \le \lfloor s/2 \rfloor.
  • b≤cb \le c, i.e., s−a≤cs - a \le c, hence a≥s−ca \ge s - c.
  • a≥1a \ge 1.

Therefore aa ranges over the integer interval [max⁡(1,s−c),  ⌊s/2⌋][\max(1, s-c),\; \lfloor s/2 \rfloor]. The number of integers in this interval is ⌊s/2⌋−max⁡(1,s−c)+1\lfloor s/2 \rfloor - \max(1, s-c) + 1 when the interval is non-empty (i.e., when this expression is positive), and zero otherwise. □\square

Theorem 2 (Cumulative count). The total number of cuboids with 1≤a≤b≤c≤M1 \le a \le b \le c \le M having integer shortest surface path is

Count(M)=∑c=1M∑s=2s2+c2=□2cN(s,c).\mathrm{Count}(M) = \sum_{c=1}^{M} \sum_{\substack{s=2 \\ s^2 + c^2 = \square}}^{2c} N(s, c).

Proof. For each largest dimension c∈{1,…,M}c \in \{1, \ldots, M\}, the sum s=a+bs = a + b ranges over {2,…,2c}\{2, \ldots, 2c\} (since 1≤a≤b≤c1 \le a \le b \le c implies 2≤s≤2c2 \le s \le 2c). We include only those ss for which s2+c2s^2 + c^2 is a perfect square (Lemma 1). For each qualifying (s,c)(s, c), Lemma 2 gives the count of valid (a,b)(a, b) decompositions. Summing over all cc yields the total. □\square

Editorial

Once the cuboid is ordered as a≤b≤ca \le b \le c, the shortest surface route depends only on cc and on the sum s=a+bs = a + b. That collapses the geometric problem into a number-theoretic one: we only need to know when s2+c2s^2 + c^2 is a perfect square.

The implementation grows the largest side cc one value at a time. For that fixed cc, it generates every possible sum ss with 2≤s≤2c2 \le s \le 2c, keeps only the values for which the shortest path length is integral, and then counts how many pairs (a,b)(a,b) produce that same sum while respecting a≤b≤ca \le b \le c. Those decompositions are the real candidates; the perfect-square test filters which sums contribute. The first cc for which the cumulative total exceeds one million is the answer.

Pseudocode

Set the running total of valid cuboids to 0.
Set the current maximum side length to 0.

While the running total does not yet exceed one million:
    Increase the maximum side length c by 1

    For each possible sum s from 2 to 2c:
        Compute s^2 + c^2
        If this is not a perfect square, continue

        Count how many pairs a <= b <= c satisfy a + b = s
        Add that number of pairs to the running total

Return the first value of c that makes the total exceed one million.

Complexity Analysis

Time: O(M2)O(M^2) for the direct approach (checking O(c)O(c) values of ss for each c≤Mc \le M). This can be improved to O(MM)O(M\sqrt{M}) by generating Pythagorean triples via the parametrization s=k(m2−n2)s = k(m^2 - n^2), c=2kmnc = 2kmn (and the symmetric role), for coprime m>n≥1m > n \ge 1 with m−nm - n odd.

Space: O(1)O(1) auxiliary.

Answer

1818\boxed{1818}

Code

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

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

int main() {
    // Problem 86: Cuboid Route
    // Shortest path for a <= b <= c: sqrt((a+b)^2 + c^2)
    // Let s = a+b; need s^2 + c^2 = perfect square.
    // Count of valid (a,b): floor(s/2) - max(1, s-c) + 1.
    // Answer: 1818

    int total = 0;
    int M = 0;

    while (total <= 1000000) {
        M++;
        int c = M;
        for (int s = 2; s <= 2 * c; s++) {
            long long dsq = (long long)s * s + (long long)c * c;
            long long d = (long long)sqrt((double)dsq);
            while (d * d < dsq) d++;
            while (d * d > dsq) d--;
            if (d * d == dsq) {
                int lo = max(1, s - c);
                int hi = s / 2;
                if (hi >= lo) {
                    total += hi - lo + 1;
                }
            }
        }
    }

    cout << M << endl;
    return 0;
}