Pattern in Number Spirals
So I was doing Project Euler #28 and almost just wrote a loop to brute-force it, but the spiral itself looked too structured to leave alone. Turns out you can skip the whole simulation and get there with algebra. Writing it down here before I forget how I got there.
The problem
Start at 1 and spiral outward, filling in numbers as you go:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
The diagonals of this spiral (going both directions through the center) hold some interesting numbers. The question: what's the sum of both diagonals in a 1001×1001 version of this spiral?
You could just build the spiral out to 1001×1001 and add up the diagonal cells. It'd work fine. But I wanted to know if there was a shortcut, and there is.
Breaking it into layers
If you squint at the spiral, it's really just a series of nested square "rings" wrapped around the center. I'll call the ring index l, the layer:
\(l = 0\) → just the number 1, sitting alone
\(l = 1\) → the ring that makes up the 3×3 square
\(l = 2\) → the ring that makes up the 5×5 square
and so on
Each layer l corresponds to a square of side length \((2l+1)\), and since the spiral fills in numbers in order, the top-right corner of that square is always the largest number in it — which just means it equals the square's area:
$$\text{Corner}_1 = (2l+1)^2 = 4l^2 + 4l + 1$$
That's a nice starting point. But what really makes this click is the second observation: all four corners of a given ring are evenly spaced. Walking around the ring corner to corner, you're always stepping by the same amount — the side length minus 1, or 2l. So once you know one corner, you know all four just by subtracting 2l repeatedly:
$$\text{Corner 1 (top-right)}: 4l^2 + 4l + 1$$
$$\text{Corner 2 (top-left)}: 4l^2 + 2l + 1$$
$$\text{Corner 3 (bottom-left)}: 4l^2 + 1$$
$$\text{Corner 4 (bottom-right)}: 4l^2 - 2l + 1$$
Add those four together, and the cross terms fall away nicely:
$$S_l = (4l^2+4l+1) + (4l^2+2l+1) + (4l^2+1) + (4l^2-2l+1) = 16l^2 + 4l + 4$$
One clean quadratic per layer. That felt almost too tidy, so before trusting it I checked it against the actual spiral.
Layer 1 (the 3×3 ring):
$$S_1 = 16(1) + 4(1) + 4 = 24$$
Actual corners: \(3 + 5 + 7 + 9 = 24\)
Layer 2 (the 5×5 ring):
$$S_2 = 16(4) + 8 + 4 = 76$$
Actual corners: \(13 + 17 + 21 + 25 = 76\)
Both check out, so the pattern holds — at least well enough to build on. So, the total sum of the diagonals will be \(1 + 24 + 76 = 101\), which is correct.
Scaling up to 1001×1001
A 1001×1001 spiral has \(2l+1 = 1001\), so the outermost layer is \(l = 500\). The total diagonal sum is just the center (1) plus every ring's contribution:
$$\text{Total} = 1 + \sum_{l=1}^{500} \left(16l^2 + 4l + 4\right)$$
Splitting the sum apart and using the standard identities
$$\sum_{l=1}^{n} l = \frac{n(n+1)}{2}, \qquad \sum_{l=1}^{n} l^2 = \frac{n(n+1)(2n+1)}{6}$$
with \(n = 500\):
$$\sum_{l=1}^{500} l^2 = \frac{500 \cdot 501 \cdot 1001}{6} = 41{,}791{,}750 $$ $$ \sum_{l=1}^{500} l = \frac{500 \cdot 501}{2} = 125{,}250$$
Plugging back in:
$$\text{Total} = 1 + 16(41{,}791{,}750) + 4(125{,}250) + 4(500) $$ $$ = 1 + 668{,}668{,}000 + 501{,}000 + 2{,}000 = \boxed{669{,}171{,}001}$$
That's the correct answer, solved without loops.
Why I like this one
What I liked about this one is that a problem which looks like it demands simulation turns out to have a closed form once you notice the corners are just an arithmetic sequence. Half of "generate and sum" type problems are like this if you look for it.
If you're trying this yourself, do what I did and check the formula against a small spiral by hand (3×3, 5×5) before trusting it at scale. Cheap way to catch an off-by-one before it wrecks the final number.