All Euler problems
Project Euler

Number Spiral Diagonals

Starting with the number 1 at the center and spiraling clockwise, a number spiral is formed. Find the sum of the numbers on the diagonals of a 1001 x 1001 spiral.

Source sync Apr 19, 2026
Problem #0028
Level Level 01
Solved By 117,996
Languages C++, Python
Answer 669171001
Length 359 words
grapharithmetic

Problem Statement

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

Starting with the number \(1\) and moving to the right in a clockwise direction a \(5\) by \(5\) spiral is formed as follows: \[ \begin {tabular}{ccccc} \textcolor {red}{21} & 22 & 23 & 24 & \textcolor {red}{25} \\ 20 & \textcolor {red}{7} & 8 & \textcolor {red}{9} & 10 \\ 19 & 6 & \textcolor {red}{1} & 2 & 11 \\ 18 & \textcolor {red}{5} & 4 & \textcolor {red}{3} & 12 \\ \textcolor {red}{17} & 16 & 15 & 14 & \textcolor {red}{13} \end {tabular} \] It can be verified that the sum of the numbers on the diagonals is \(101\).

What is the sum of the numbers on the diagonals in a \(1001\) by \(1001\) spiral formed in the same way?

Problem 28: Number Spiral Diagonals

Mathematical Development

Definition 1 (Ulam Spiral Rings). In the number spiral, ring kk (k1k \geq 1) consists of the numbers occupying the layer whose outermost side has length 2k+12k + 1. Ring 0 is the center cell containing 1. An N×NN \times N spiral (with NN odd) has n=(N1)/2n = (N-1)/2 rings surrounding the center.

Lemma 1 (Ring Boundaries). Ring kk (k1k \geq 1) contains the integers from (2k1)2+1(2k-1)^2 + 1 to (2k+1)2(2k+1)^2. The total count of integers in ring kk is 8k8k.

Proof. Ring k1k-1 ends at (2(k1)+1)2=(2k1)2(2(k-1)+1)^2 = (2k-1)^2, so ring kk begins at (2k1)2+1(2k-1)^2 + 1. It ends at (2k+1)2(2k+1)^2, the last number placed on the ring. The count is (2k+1)2(2k1)2=(4k2+4k+1)(4k24k+1)=8k(2k+1)^2 - (2k-1)^2 = (4k^2 + 4k + 1) - (4k^2 - 4k + 1) = 8k. \square

Theorem 1 (Corner Values). The four diagonal entries (corners) of ring kk (k1k \geq 1) are:

TR(k)=(2k+1)2,TL(k)=(2k+1)22k,\mathrm{TR}(k) = (2k+1)^2, \qquad \mathrm{TL}(k) = (2k+1)^2 - 2k, BL(k)=(2k+1)24k,BR(k)=(2k+1)26k.\mathrm{BL}(k) = (2k+1)^2 - 4k, \qquad \mathrm{BR}(k) = (2k+1)^2 - 6k.

Proof. Ring kk has 4 sides, each of length 2k+12k+1. Adjacent sides share a corner vertex, so each side contributes 2k2k new numbers. The spiral deposits the last number of ring kk at the top-right corner, giving TR(k)=(2k+1)2\mathrm{TR}(k) = (2k+1)^2. Proceeding counterclockwise (i.e., backward through the spiral), each successive corner is 2k2k steps earlier:

  • Top-left: (2k+1)22k(2k+1)^2 - 2k
  • Bottom-left: (2k+1)24k(2k+1)^2 - 4k
  • Bottom-right: (2k+1)26k(2k+1)^2 - 6k. \square

Theorem 2 (Ring Diagonal Sum). The sum of the four corner values of ring kk is Sk=16k2+4k+4S_k = 16k^2 + 4k + 4.

Proof.

Sk=j=03[(2k+1)22jk]=4(2k+1)22k(0+1+2+3)=4(2k+1)212k.S_k = \sum_{j=0}^{3}\bigl[(2k+1)^2 - 2jk\bigr] = 4(2k+1)^2 - 2k(0+1+2+3) = 4(2k+1)^2 - 12k.

Expanding: 4(4k2+4k+1)12k=16k2+16k+412k=16k2+4k+44(4k^2 + 4k + 1) - 12k = 16k^2 + 16k + 4 - 12k = 16k^2 + 4k + 4. \square

Theorem 3 (Closed-Form Formula). For an N×NN \times N spiral with NN odd, the sum of all diagonal entries is

S(N)=4N3+3N2+8N96.S(N) = \frac{4N^3 + 3N^2 + 8N - 9}{6}.

Proof. Let n=(N1)/2n = (N-1)/2. The total diagonal sum is

S=1+k=1nSk=1+k=1n(16k2+4k+4).S = 1 + \sum_{k=1}^{n} S_k = 1 + \sum_{k=1}^{n}(16k^2 + 4k + 4).

Applying the standard summation identities k=1nk2=n(n+1)(2n+1)6\sum_{k=1}^{n} k^2 = \frac{n(n+1)(2n+1)}{6} and k=1nk=n(n+1)2\sum_{k=1}^{n} k = \frac{n(n+1)}{2}:

S=1+16n(n+1)(2n+1)6+4n(n+1)2+4n=1+8n(n+1)(2n+1)3+2n(n+1)+4n.S = 1 + 16 \cdot \frac{n(n+1)(2n+1)}{6} + 4 \cdot \frac{n(n+1)}{2} + 4n = 1 + \frac{8n(n+1)(2n+1)}{3} + 2n(n+1) + 4n.

Substituting n=(N1)/2n = (N-1)/2, so that n+1=(N+1)/2n+1 = (N+1)/2 and 2n+1=N2n+1 = N:

S=1+8N12N+12N3+2N12N+12+4N12S = 1 + \frac{8 \cdot \frac{N-1}{2} \cdot \frac{N+1}{2} \cdot N}{3} + 2 \cdot \frac{N-1}{2} \cdot \frac{N+1}{2} + 4 \cdot \frac{N-1}{2} =1+2N(N21)3+N212+2(N1).= 1 + \frac{2N(N^2-1)}{3} + \frac{N^2-1}{2} + 2(N-1).

Finding a common denominator of 6:

S=6+4N(N21)+3(N21)+12(N1)6=6+4N34N+3N23+12N126S = \frac{6 + 4N(N^2-1) + 3(N^2-1) + 12(N-1)}{6} = \frac{6 + 4N^3 - 4N + 3N^2 - 3 + 12N - 12}{6} =4N3+3N2+8N96.= \frac{4N^3 + 3N^2 + 8N - 9}{6}. \qquad\square

Lemma 2 (Verification). The formula is confirmed for small cases:

  • N=1N = 1: S=(4+3+89)/6=1S = (4 + 3 + 8 - 9)/6 = 1. Correct (center only).
  • N=3N = 3: S=(108+27+249)/6=150/6=25S = (108 + 27 + 24 - 9)/6 = 150/6 = 25. Diagonal entries: {1,3,5,7,9}\{1, 3, 5, 7, 9\}. Sum =25= 25.
  • N=5N = 5: S=(500+75+409)/6=606/6=101S = (500 + 75 + 40 - 9)/6 = 606/6 = 101. Matches the problem statement.

Editorial

We evaluate the closed-form expression obtained from summing the four corner values contributed by each spiral layer. That gives the answer in constant time; the iterative layer-by-layer accumulation is retained as a verification method. This is sufficient because every diagonal entry belongs either to the center or to exactly one square ring.

Pseudocode

Algorithm: Spiral Diagonal Sum by Closed Form
Require: An odd integer N ≥ 1.
Ensure: The sum of the diagonal entries in the N x N number spiral.
1: Set m ← (N - 1) / 2.
2: Evaluate S ← 1 + ∑_{k=1}^m (16k^2 + 4k + 4).
3: Equivalently evaluate the simplified closed form S ← (4N^3 + 3N^2 + 8N - 9) / 6.
4: Return S.
Algorithm: Spiral Diagonal Sum by Ring Accumulation
Require: An odd integer N ≥ 1.
Ensure: The sum of the diagonal entries in the N x N number spiral.
1: Initialize S ← 1.
2: For each ring index k in {1, 2, ..., (N - 1) / 2} do:
3:     Compute the corner contribution C_k ← 16k^2 + 4k + 4 and update S ← S + C_k.
4: Return S.

Complexity Analysis

Proposition. The closed-form algorithm runs in O(1)O(1) time and O(1)O(1) space. The iterative algorithm runs in O(N)O(N) time and O(1)O(1) space.

Proof. The closed-form expression evaluates a fixed number of arithmetic operations. The iterative version performs (N1)/2(N-1)/2 loop iterations, each with O(1)O(1) arithmetic. Neither requires auxiliary storage beyond a constant number of variables. \square

Answer

669171001\boxed{669171001}

Code

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

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

int main() {
    long long N = 1001;
    // Closed-form: S = (4N^3 + 3N^2 + 8N - 9) / 6
    long long answer = (4*N*N*N + 3*N*N + 8*N - 9) / 6;
    cout << answer << endl;
    return 0;
}