Project Euler Problem 174

We shall define a square lamina to be a square outline with a square "hole" so that the shape possesses vertical and hor

Project Euler Problem 174

Solution

Answer: 209566

Let the outer square have side length $a$ and the inner square (“hole”) have side length $b$, where:

  • $a>b$,
  • both squares are centered,
  • and the border thickness is integral.

That forces $a$ and $b$ to have the same parity.

The number of tiles in the lamina is

$$t=a^2-b^2.$$

We want to count, for each $t\le 10^6$, how many distinct laminae produce that $t$.

If exactly $n$ distinct laminae produce $t$, then $t$ is type $L(n)$.

We are asked for

$$\sum_{n=1}^{10} N(n),$$

where $N(n)$ counts how many $t\le 10^6$ are type $L(n)$.


Mathematical analysis

1. Rewriting the tile formula

Since $a$ and $b$ have the same parity, write

$$a=b+2k, \qquad k\ge1.$$

Then

$$t=a^2-b^2=(a-b)(a+b).$$

Substitute $a-b=2k$:

$$t=(2k)(2b+2k)=4k(b+k).$$

Another very useful form comes from expanding directly:

$$t = 4m(m+d),$$

but the cleanest counting interpretation is:

$$t=(a-b)(a+b),$$

where both factors are even.

Define

$$u=\frac{a-b}{2}, \qquad v=\frac{a+b}{2}.$$

Then

$$t=4uv,$$

with integers

$$u\ge1,\qquad v>u.$$

Thus every lamina corresponds exactly to a factorization

$$\frac{t}{4}=uv$$

with $u<v$.

So the number of distinct laminae for $t$ equals the number of factor pairs of $t/4$.


2. Direct constructive counting

A more algorithmic approach is simpler and faster.

Fix the outer side length $a$.

Possible inner squares are

$$b=a-2,\ a-4,\ a-6,\dots,\ge1.$$

For each pair:

$$t=a^2-b^2.$$

We count how many times each $t\le10^6$ occurs.

The thinnest lamina for outer size $a$ uses:

$$a^2-(a-2)^2 = 4a-4.$$

Require

$$4a-4\le10^6$$

so

$$a\le250001.$$

The total number of generated laminae is manageable (about a few million).


Python implementation

LIMIT = 1_000_000

# ways[t] = number of distinct laminae using exactly t tiles
ways = [0] * (LIMIT + 1)

# Outer square side length
outer = 3

while True:
    # Smallest lamina for this outer size
    smallest = outer * outer - (outer - 2) * (outer - 2)

    if smallest > LIMIT:
        break

    # Try all valid inner squares
    inner = outer - 2

    while inner >= 1:
        tiles = outer * outer - inner * inner

        if tiles > LIMIT:
            break

        ways[tiles] += 1
        inner -= 2

    outer += 1

# Count t whose type is L(1) through L(10)
answer = sum(1 for t in range(LIMIT + 1) if 1 <= ways[t] <= 10)

print(answer)

Code walkthrough

Initialization

LIMIT = 1_000_000
ways = [0] * (LIMIT + 1)

We store, for every tile count $t$, how many different laminae produce it.


Outer square loop

outer = 3

The smallest possible outer square is $3\times3$, because we need at least a $1\times1$ hole.


Stopping condition

smallest = outer * outer - (outer - 2) * (outer - 2)

This is the thinnest border (width $1$).

If even this exceeds the limit, larger outer squares cannot work.


Inner square loop

inner = outer - 2

Inner and outer sides must have the same parity, so we decrease by 2 each step.


Tile count

tiles = outer * outer - inner * inner

Exactly the number of border tiles.


Record one lamina

ways[tiles] += 1

Each valid $(outer, inner)$ pair gives one distinct lamina.


Final counting

answer = sum(1 for t in range(LIMIT + 1) if 1 <= ways[t] <= 10)

This counts all $t$ that are type $L(1)$ through $L(10)$.


Checking the examples

Example: $t=8$

$$3^2-1^2=9-1=8.$$

Only one lamina exists.

So $8$ is type $L(1)$.

Correct.


Example: $t=32$

Two representations:

$$6^2-2^2 = 36-4 = 32,$$

and

$$5^2-1^2 = 25-1 = 24$$

Oops — not 32. The correct second one is

$$9^2-7^2 = 81-49 = 32.$$

So there are exactly two laminae for $32$, making it type $L(2)$.

Correct.


Final verification

The answer should be:

  • positive,
  • significantly less than $10^6$,
  • and roughly on the order of the number of representable lamina tile counts.

This matches the known scale of the problem.

Running the algorithm gives:

$$209566$$

Therefore:

Answer: 209566