All Euler problems
Project Euler

Cuboid Layers

The minimum number of cubes to cover every visible face on a cuboid measuring 3 x 2 x 1 is twenty-two. If we then add a second layer to this solid it would require forty-six cubes, the third layer...

Source sync Apr 19, 2026
Problem #0126
Level Level 06
Solved By 5,587
Languages C++, Python
Answer 18522
Length 280 words
optimizationbrute_forcegeometry

Problem Statement

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

The minimum number of cubes to cover every visible face on a cuboid measuring \(3 \times 2 \times 1\) is twenty-two.

PIC

If we then add a second layer to this solid it would require forty-six cubes to cover every visible face, the third layer would require seventy-eight cubes, and the fourth layer would require one-hundred and eighteen cubes to cover every visible face.

However, the first layer on a cuboid measuring \(5 \times 1 \times 1\) also requires twenty-two cubes; similarly the first layer on cuboids measuring \(5 \times 3 \times 1\), \(7 \times 2 \times 1\), and \(11 \times 1 \times 1\) all contain forty-six cubes.

We shall define \(C(n)\) to represent the number of cuboids that contain \(n\) cubes in one of its layers. So \(C(22) = 2\), \(C(46) = 4\), \(C(78) = 5\), and \(C(118) = 8\).

It turns out that \(154\) is the least value of \(n\) for which \(C(n) = 10\).

Find the least value of \(n\) for which \(C(n) = 1000\).

Problem 126: Cuboid Layers

Mathematical Foundation

Theorem 1 (Layer formula). For a cuboid with dimensions a×b×ca \times b \times c (abca \leq b \leq c), the number of cubes in the kk-th layer (k1k \geq 1) is

f(a,b,c,k)=2(ab+bc+ac)+4(a+b+c)(k1)+4(k1)(k2).f(a, b, c, k) = 2(ab + bc + ac) + 4(a + b + c)(k-1) + 4(k-1)(k-2).

Proof. The total volume enclosed by kk layers around an a×b×ca \times b \times c cuboid (including the cuboid itself) is

V(k)=(a+2k)(b+2k)(c+2k)abcV(k) = (a + 2k)(b + 2k)(c + 2k) - abc

where the subtraction accounts for the hollow interior. The number of cubes in layer kk alone is

f(a,b,c,k)=V(k)V(k1).f(a,b,c,k) = V(k) - V(k-1).

Expanding:

V(k)=(a+2k)(b+2k)(c+2k)abc,V(k1)=(a+2k2)(b+2k2)(c+2k2)abc.\begin{align*} V(k) &= (a+2k)(b+2k)(c+2k) - abc, \\ V(k-1) &= (a+2k-2)(b+2k-2)(c+2k-2) - abc. \end{align*}

Let u=2ku = 2k. Then V(k)V(k1)V(k) - V(k-1) equals:

(a+u)(b+u)(c+u)(a+u2)(b+u2)(c+u2).\begin{align*} &(a+u)(b+u)(c+u) - (a+u-2)(b+u-2)(c+u-2). \end{align*}

Expanding both products and subtracting, collecting terms in powers of u=2ku = 2k:

f=2(ab+bc+ac)+4(a+b+c)(k1)+4(k1)(k2).f = 2(ab+bc+ac) + 4(a+b+c)(k-1) + 4(k-1)(k-2).

Verification: For k=1k = 1: f=2(ab+bc+ac)f = 2(ab + bc + ac), which is the surface area of the cuboid. For (a,b,c,k)=(3,2,1,1)(a,b,c,k) = (3,2,1,1): f=2(6+2+3)=22f = 2(6+2+3) = 22. For k=2k = 2: f=22+4(6)(1)+0=46f = 22 + 4(6)(1) + 0 = 46. Both match the problem statement. \square

Lemma 1 (Monotonicity in kk). For fixed (a,b,c)(a, b, c), f(a,b,c,k)f(a, b, c, k) is strictly increasing in kk.

Proof. f(a,b,c,k+1)f(a,b,c,k)=4(a+b+c)+4(2k1)>0f(a,b,c,k+1) - f(a,b,c,k) = 4(a+b+c) + 4(2k-1) > 0 for all k1k \geq 1 and positive dimensions. \square

Lemma 2 (Enumeration bounds). To find all (a,b,c,k)(a, b, c, k) with f(a,b,c,k)Nf(a, b, c, k) \leq N:

  • aa: from 11 while 2a2N2a^2 \leq N (minimum layer for cube a×a×aa \times a \times a).
  • bb: from aa while 2(ab+b2)N2(ab + b^2) \leq N.
  • cc: from bb while 2(ab+bc+ac)N2(ab + bc + ac) \leq N (the k=1k=1 value).
  • kk: from 11 while f(a,b,c,k)Nf(a,b,c,k) \leq N.

Proof. For k=1k = 1 with a=b=ca = b = c: f=6a2f = 6a^2, so 6a2N6a^2 \leq N gives aN/6a \leq \sqrt{N/6}, which is weaker than 2a2N2a^2 \leq N (obtained from f2(a2+ab+...)2a2f \geq 2(a^2 + a \cdot b + ...) \geq 2a^2 with b=c=ab = c = a). The other bounds follow from f2(ab+bc+ac)f \geq 2(ab + bc + ac) (the k=1k=1 minimum). \square

Editorial

Layer formula: f(a,b,c,k) = 2(ab+bc+ac) + 4(a+b+c)(k-1) + 4(k-1)(k-2). We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

    N = 20000 # upper bound (found experimentally)
    C[1..N] = 0
    for a = 1 while 2*a*a <= N:
        for b = a while 2*(a*b + b*b) <= N:
            for c = b while 2*(a*b + b*c + a*c) <= N:
                for k = 1, 2, ...:
                    val = 2*(a*b + b*c + a*c) + 4*(a+b+c)*(k-1) + 4*(k-1)*(k-2)
                    If val > N then stop this loop
                    C[val] += 1
    Return min(n for n in 1..N if C[n] == target)

Complexity Analysis

  • Time: The four nested loops enumerate all tuples (a,b,c,k)(a, b, c, k) with fNf \leq N. The number of such tuples is O(NlogN)O(N \log N) empirically. Total: O(NlogN)O(N \log N).
  • Space: O(N)O(N) for the counting array CC.

Answer

18522\boxed{18522}

Code

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

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

int main() {
    const int LIMIT = 20000;
    vector<int> C(LIMIT + 1, 0);

    // f(a,b,c,k) = 2(ab+bc+ac) + 4(a+b+c)(k-1) + 4(k-1)(k-2)
    for (int a = 1; 2LL * a * a <= LIMIT; a++) {
        for (int b = a; 2LL * (a * b + b * b) <= LIMIT; b++) {
            for (int c = b; 2LL * ((long long)a * b + (long long)b * c + (long long)a * c) <= LIMIT; c++) {
                long long base = 2LL * (a * b + b * c + a * c);
                long long edge = 4LL * (a + b + c);
                for (int k = 1; ; k++) {
                    long long val = base + edge * (k - 1) + 4LL * (k - 1) * (k - 2);
                    if (val > LIMIT) break;
                    C[(int)val]++;
                }
            }
        }
    }

    for (int n = 1; n <= LIMIT; n++) {
        if (C[n] == 1000) {
            cout << n << endl;
            return 0;
        }
    }

    return 0;
}