All Euler problems
Project Euler

Using Up to One Million Tiles How Many Different "Hollow" Square Laminae Can Be Formed?

A hollow square lamina is formed by removing a smaller concentric square hole from a larger square. Using up to N = 10^6 unit-square tiles, how many distinct hollow square laminae can be formed?

Source sync Apr 19, 2026
Problem #0173
Level Level 04
Solved By 10,245
Languages C++, Python
Answer 1572729
Length 320 words
modular_arithmeticoptimizationbrute_force

Problem Statement

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

We shall define a square lamina to be a square outline with a square "hole" so that the shape possesses vertical and horizontal symmetry. For example, using exactly thirty-two square tiles we can form two different square laminae:

PIC

With one-hundred tiles, and not necessarily using all of the tiles at one time, it is possible to form forty-one different square laminae.

Using up to one million tiles how many different square laminae can be formed?

Problem 173: Using Up to One Million Tiles How Many Different “Hollow” Square Laminae Can Be Formed?

Mathematical Development

Definition 1. A hollow square lamina is parameterized by a pair (a,b)(a, b) of positive integers with a>b1a > b \geq 1 and ab(mod2)a \equiv b \pmod{2}, where aa is the outer side length, bb is the inner side length, and the congruence condition ensures the hole is centered (uniform border width (ab)/2(a-b)/2).

Theorem 1 (Tile count). A hollow square lamina (a,b)(a, b) uses exactly t(a,b)=a2b2t(a, b) = a^2 - b^2 tiles.

Proof. The lamina occupies a2a^2 unit cells minus the b2b^2 cells removed for the interior hole. \square

Lemma 1 (Factored form). Setting k=(ab)/21k = (a - b)/2 \geq 1 (the border width), the tile count becomes t=4k(ak)t = 4k(a - k). For fixed border width kk, the outer side aa ranges over integers a2k+1a \geq 2k + 1 (so that b=a2k1b = a - 2k \geq 1), subject to 4k(ak)N4k(a - k) \leq N.

Proof. We have t=a2b2=(ab)(a+b)t = a^2 - b^2 = (a - b)(a + b). With ab=2ka - b = 2k, we obtain a+b=2a2ka + b = 2a - 2k, so t=2k(2a2k)=4k(ak)t = 2k(2a - 2k) = 4k(a - k). The constraint b1b \geq 1 gives a2k1a - 2k \geq 1, i.e., a2k+1a \geq 2k + 1. \square

Lemma 2 (Range of the outer side). The minimum tile count for a given outer side aa is achieved at b=a2b = a - 2 (border width k=1k = 1), giving tmin(a)=a2(a2)2=4(a1)t_{\min}(a) = a^2 - (a-2)^2 = 4(a-1). Hence a necessary condition for a lamina with outer side aa to exist is aN/4+1a \leq N/4 + 1.

Proof. The largest allowable inner side is bmax=a2b_{\max} = a - 2, which minimizes a2b2a^2 - b^2. Computing: 4(a1)N4(a-1) \leq N iff aN/4+1a \leq N/4 + 1. \square

Theorem 2 (Counting formula). For each outer side a3a \geq 3 with 4(a1)N4(a-1) \leq N, the number of valid inner sides bb is

count(a)=bmaxbmin2+1,\mathrm{count}(a) = \left\lfloor \frac{b_{\max} - b_{\min}}{2} \right\rfloor + 1,

where bmax=a2b_{\max} = a - 2 and bminb_{\min} is the smallest positive integer satisfying bmina(mod2)b_{\min} \equiv a \pmod{2} and a2bmin2Na^2 - b_{\min}^2 \leq N. If no such bb exists (i.e., bmin>bmaxb_{\min} > b_{\max}), then count(a)=0\mathrm{count}(a) = 0.

Proof. The valid values of bb form an arithmetic progression {bmin,bmin+2,bmin+4,,bmax}\{b_{\min}, b_{\min}+2, b_{\min}+4, \ldots, b_{\max}\} with common difference 2. The number of terms in such a progression with first term bminb_{\min} and last term bmaxb_{\max} is (bmaxbmin)/2+1\lfloor(b_{\max} - b_{\min})/2\rfloor + 1. \square

Corollary 1. The total number of laminae is a=3N/4+1count(a)\sum_{a=3}^{\lfloor N/4+1 \rfloor} \mathrm{count}(a).

Editorial

Count distinct hollow square laminae using at most N = 10^6 tiles. Lamina (a, b): outer side a, inner side b, same parity, tiles = a^2 - b^2. We else.

Pseudocode

N = 10^6
total = 0
for a = 3, 4, 5, ...:
    If 4*(a - 1) > N then stop this loop
    b_max = a - 2
    lo = a*a - N
    If lo <= 1 then
        b_min = (2 if a is even else 1)
    else:
        b_min = ceil(sqrt(lo))
        if b_min % 2 != a % 2: b_min += 1
    If b_min <= b_max then
        total += (b_max - b_min) // 2 + 1
Return total

Complexity Analysis

  • Time: The outer loop runs for a=3,4,,N/4+1=250001a = 3, 4, \ldots, \lfloor N/4 + 1 \rfloor = 250001, giving O(N)O(N) iterations, each performing O(1)O(1) arithmetic (one integer square root and constant-many comparisons).
  • Space: O(1)O(1).

Answer

1572729\boxed{1572729}

Code

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

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

/*
 * Problem 173: Count hollow square laminae with at most N = 10^6 tiles.
 * Lamina (a, b): outer side a, inner side b, same parity, tiles = a^2 - b^2.
 * For each a >= 3, count valid inner sides b.
 */

int main() {
    long long N = 1000000;
    long long total = 0;

    for (long long a = 3; 4 * (a - 1) <= N; a++) {
        long long b_max = a - 2;
        long long lo = a * a - N;
        long long b_min;
        if (lo <= 1) {
            b_min = (a % 2 == 0) ? 2 : 1;
        } else {
            b_min = (long long)ceil(sqrt((double)lo));
            while (b_min * b_min < lo) b_min++;
            if (b_min % 2 != a % 2) b_min++;
        }
        if (b_min > b_max) continue;
        total += (b_max - b_min) / 2 + 1;
    }

    cout << total << endl;
    return 0;
}