Problem Description
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
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
It can be verified that the sum of both diagonals is 101.
What is the sum of both diagonals in a 1001 by 1001 spiral formed in the same way?
Analysis
The “corners” which form the central diagonals form a simple series: (3, 5, 7, 9), (13, 17, 21, 25), (31, 37, 43, 49), …
Simply add the series for [2,1001] iterations, stepping by 2 since you can’t have even numbered sides (2001 terms).
With the exception of the center, which is one, the four corners form another series when added together: 24, 76, 160, … for odd n>2 is 4n2 − 6n + 6. Or, you could even take this one step further and summarize the sum for both diagonals based on the length of a side in one equation.
Solution
Runs < 1 second in Python.
sum, d, size = 1, 1, 1001 for n in range(2, size, 2): sum += 4*d + 10*n d += n*4; print "Answer to PE28 = ",sum
Comments
The 4*d + 10*n replaces a loop that looks like Sum += (d + n*i) for i (1..4)
See problem 58.





[...] Note how we calculate the answer as n-1. This is because we count only even n. See problem 28. [...]