Number Spiral Diagonals¶

Whether you choose to program or just use pen and paper, there's a lot of different ways to tackle this problem. Here's one approach.

If we have an $n \times n$ spiral (note that $n$ must be odd!), it's pretty easy to get a formula for just the sum of the corners. The top right corner will always be $n^2$, and since it's an $n \times n$ spiral (as mentioned one sentence ago), the top left corner will just be $n^2 - (n - 1)$. We can continue subtracting $n-1$ to get the values of the other corners. Adding these up, we get $$f(n) = n^2 + (n^2 - (n-1)) + (n^2 - 2(n-1)) + (n^2 - 3(n-1)) = 4n^2 - 6n + 6$$

Of course, we want the sums of the diagonals, not just the outermost corners. We can think about this as getting the sum of the corners of the $n \times n$ spiral, plus the sum of the corners of the $(n-2) \times (n-2)$ spiral one layer deeper, and so on until we reach the 1 at the center. $$g(n) = f(n) + f(n-2) + f(n-4) + \cdots + f(5) + f(3) + 1 = 1 + \sum_{k=1}^{(n-1)/2} f(2k + 1) = 1 + \sum_{k=1}^{(n-1)/2} (16k^2 + 4k + 4)$$

Now we can apply summation identities (including the return of triangular numbers and square pyramidal numbers from problem 6) to get a closed formula: $$g(n) = \frac{2}{3}n^3 + \frac{1}{2}n^2 + \frac{4}{3}n - \frac{3}{2}$$ Plugging in 1001, we get our answer: $g(1001) = 669171001$.

Side note: this practice of writing the natural numbers in a spiral, combined with marking the prime numbers, has been coined the Ulam spiral. Somewhat interestingly, lots of primes appear in vertical, horizontal, and diagonal lines when laid out this way.

Relevant sequences¶

  • Numbers on diagonals: A200975

Copyright (C) 2025 filifa¶

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.