In [ ]:
"""
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?
"""

"""
This seems to be Ulam's Spiral. 
https://oeis.org/search?q=1%2C3%2C5%2C7%2C9%2C13%2C17%2C21%2C25%2C31%2C37&sort=&language=english&go=Search

Formula (c) Bob Selcoe: a(n) = floor_(n*(n+2)/4) + floor_(n(mod 4)/3) + 1

n = math.floor(n*(n+2)/4) + math.floor((n%4)/3) + 1 

also: 

For k>=0, the sequence is all numbers of the following three forms: 
(2k+1)^2, 4*k^2+1 and k^2+k+1.  When k>=1, all values of a(n) are 
uniquely generated by these three forms.

When k>=1 and n>=2, a(n) is:

1. (2k+1)^2 when n==1(mod 4);
2. 4*k^2+1 when n==3(mod 4); and
3  k^2+k+1 when n is even.

Anyway, seems easier to do (n+2) every 4 times
"""

In [1]:
from math import floor

n=2

while n < 50:
    n = floor(n*(n+2)/4) + floor((n%4)/3) + 1 
    print(n)


3
5
9
25
169

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: