This assignment of Algorithms Analysis discipline has a very simple source code. The challenge here is to derive a O(1) complexity solution for the points to build the Ulam spiral.

My solution (x,y) to the point n is:

\[\begin{align} & b = \lfloor \sqrt{n} \rfloor \\ & h = \lfloor{\frac{b + 1}{2}}\rfloor \\ & d = n - b^{2} + (b\ \ \textrm{mod}\ \ 2) \\ & dx = \begin{cases} 2h & \textrm{if}\ d > 2h \\ d & \textrm{otherwise} \end{cases} \\ & dy = \begin{cases} d - 2h & \textrm{if}\ d > 2h \\ 0 & \textrm{otherwise} \end{cases} \\ & s = -1^{b\ \ \textrm{mod}\ \ 2} \\ & x = s (dx - h) \\ & y = s (dy - h) \\ \end{align}\]

Here is a video of the program running for n=999:


Source code available on GitHub

Updated: