Number spiral diagonals

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 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?


Idea

Find the rule of diagonals, then all is obvious.


In [1]:
def solve(width):
    assert width % 2 == 1
    layers_number = width // 2
    s = 1
    layer_i = 1
    for l in range(layers_number):
        interval = 2 * (l + 1)
        for i in range(4):
            layer_i += interval
            s += layer_i
    return s
In [2]:
solve(5)
Out[2]:
101
In [3]:
solve(1001)
Out[3]:
669171001