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

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 , the shortest surface path from one corner to the diagonally opposite corner has squared length
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:
We claim whenever .
Proof of :
Define . Since for , is strictly increasing on . As , we have .
Proof of :
Since , we have , so the left side is and the right side is . Dividing by (when ) reverses the inequality to , which holds since implies . When , both sides equal zero.
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 such that , i.e., forms a Pythagorean triple.
Proof. By Theorem 1, the squared path length is . This is a perfect square if and only if the path length is a positive integer.
Counting Valid Cuboids
Lemma 2 (Valid pair count). For a Pythagorean triple with legs and (where ), the number of ordered pairs satisfying and is
Proof. We require simultaneously:
- and , which gives , hence .
- , i.e., , hence .
- .
Therefore ranges over the integer interval . The number of integers in this interval is when the interval is non-empty (i.e., when this expression is positive), and zero otherwise.
Theorem 2 (Cumulative count). The total number of cuboids with having integer shortest surface path is
Proof. For each largest dimension , the sum ranges over (since implies ). We include only those for which is a perfect square (Lemma 1). For each qualifying , Lemma 2 gives the count of valid decompositions. Summing over all yields the total.
Editorial
Once the cuboid is ordered as , the shortest surface route depends only on and on the sum . That collapses the geometric problem into a number-theoretic one: we only need to know when is a perfect square.
The implementation grows the largest side one value at a time. For that fixed , it generates every possible sum with , keeps only the values for which the shortest path length is integral, and then counts how many pairs produce that same sum while respecting . Those decompositions are the real candidates; the perfect-square test filters which sums contribute. The first 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: for the direct approach (checking values of for each ). This can be improved to by generating Pythagorean triples via the parametrization , (and the symmetric role), for coprime with odd.
Space: auxiliary.
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 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;
}
"""
Problem 86: Cuboid Route
Find least M such that the number of cuboids a <= b <= c <= M
with integer shortest surface path exceeds 1,000,000.
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) for given (s,c): floor(s/2) - max(1, s-c) + 1.
Answer: 1818
"""
import math
def solve():
total = 0
M = 0
while total <= 1_000_000:
M += 1
c = M
for s in range(2, 2 * c + 1):
dsq = s * s + c * c
d = math.isqrt(dsq)
if d * d == dsq:
lo = max(1, s - c)
hi = s // 2
if hi >= lo:
total += hi - lo + 1
print(M)
if __name__ == "__main__":
solve()