Finding the formula for the ulam spiral starting with $0$ as a bijective function $U:\mathbb{N}\rightarrow\mathbb{Z×Z}$

As the spiral is winding around, we have four $90°$ turns before completing a full rotation. Notice that each rotation starts and ends with an odd square.

This is because, starting at $r^2-1$ and adding the four sides $4(r+1)$ to complete a rotation, gives $r^2+4r+3=(r+2)^2-1$, reaching the next odd square.

Therefore, odd squares $n=(2k+1)^2$ are mapped to $(y,x)=(-k, k+1)$.

Hence, given $n$ we start by computing $k=\frac12(\sqrt{n}-1)$ to determine in which rotation the given number is located in. That is, it is located between two odd squares $(2\lfloor k\rfloor+1)^2$ and $(2\lceil k\rceil+1)^2$ that determine the start and end of the rotation.

enter image description here

Now we simply backtrack from the end to the start of the rotation.

Given $n$, end of rotation is at $(-K, K+1)$, where $K=\left\lceil\frac12(\sqrt{n}-1)\right\rceil$.

Given $n$, distance from the end of the rotation is $d=(2K+1)^2 - n$.

This gives $U(n)=(y,x)$ as:

$$ U(n)= \begin{cases} (-K&,+K+1-d&), & 0K+0\le d\le 2K+1\\ (-3K-1+d&,-K&), & 2K+1\lt d\le 4K+1\\ (+K&,-5K-1+d&), & 4K+1\lt d\le 6K+1\\ (+7K+1-d&,+K&), & 6K+1\lt d\lt 8K+1\\ \end{cases} $$

which is as simple as it gets.

Verifying the formula in python, gives the expected result as in the picture:

36  35  34  33  32  31  30      
37  16  15  14  13  12  29      
38  17  4   3   2   11  28      
39  18  5   0   1   10  27      
40  19  6   7   8   9   26      
41  20  21  22  23  24  25      
42  43  44  45  46  47  48  49 

We have the power set $\mathcal{P}(\Bbb Z \times \Bbb Z)$ and can define a transformation $\Gamma$ on the $3\text{-coordinate}$ set

$\tag 1 \mathcal U =\mathcal{P}(\Bbb Z \times \Bbb Z) \times (\Bbb Z \times \Bbb Z) \times \{-1,1\}$ by

$\quad \Gamma(S,(x,y),w))=$

$$ \begin{cases} \bigr(S \cup \{(x,y+1)\}&,(x,y+1)&,w\bigr), & w = -1 \land (x-1,y) \in S\\ \bigr(S \cup \{(x-1,y)\}&,(x-1,y)&,w\bigr), & w = -1 \land (x-1,y) \notin S \land (x-1,y-1) \in S\\ \bigr(S \cup \{(x-1,y)\}&,(x-1,y)&,-w\bigr), & w = -1 \land (x-1,y) \notin S \land (x-1,y-1) \notin S\\ \bigr(S \cup \{(x,y-1)\}&,(x,y-1)&,w\bigr), & w = 1 \land (x+1,y) \in S \\ \bigr(S \cup \{(x+1,y)\}&,(x+1,y)&,w\bigr), & w = 1 \land (x+1,y) \notin S \land (x+1,y+1) \in S\\ \bigr(S \cup \{(x+1,y)\}&,(x+1,y)&,-w\bigr), & w = 1 \land (x+1,y) \notin S \land (x+1,y+1) \notin S\\ \end{cases} $$

We have the projection mapping $\pi_2: \mathcal U \to \Bbb Z \times \Bbb Z$ onto the $2^{nd}$ coordinate.

The solution $U:\mathbb{N}\rightarrow\mathbb{Z×Z}$ is defined by

$\tag 2 U(n) = \pi_2 \circ \Gamma^{n}\big(\{(0,0)\},(0,0),1\bigr)\quad \text{for } n\ge 0$


Using interval notation we can partition the natural numbers by writing

$\tag 1 \Bbb N = \displaystyle \bigsqcup_{k \in \Bbb N} \bigr[k^2,(k+1)^2\bigr)$

Let $\phi_k$ denote the restriction of the $\text{counter-clockwise Ulam 0-spiral}$ function $U$ to $[k^2,(k+1)^2\bigr)$.

If $k$ is even then $$ \quad \phi_k(n)= \begin{cases} \bigr(-\frac{k}{2}, \frac{k}{2} - (n-k^2) \bigr), & k^2 \le n \le k^2 +k\\ \bigr(-\frac{k}{2}+(n-k^2-k), \frac{k}{2} -k \bigr), & k^2 + k + 1 \le n \lt (k+1)^2\\\\ \end{cases} $$

If $k$ is odd then $$ \quad \phi_k(n)= \begin{cases} \bigr(\frac{k+1}{2}, \frac{1-k}{2} + (n-k^2) \bigr), & k^2 \le n \le k^2 +k\\ \bigr(\frac{k+1}{2}-(n-k^2-k), \frac{1-k}{2} + k \bigr), & k^2 + k + 1 \le n \lt (k+1)^2\\\\ \end{cases} $$

After some deliberation we can write

$\quad U(n) =$ $$ \small{ \begin{cases} & \text{when } \lfloor \sqrt n \rfloor \text{ is even:}\\ \bigr(-\frac{\lfloor \sqrt n \rfloor}{2}, \frac{\lfloor \sqrt n \rfloor}{2} - (n-\lfloor \sqrt n \rfloor^2) \bigr), & \lfloor \sqrt n \rfloor^2 \le n \le \lfloor \sqrt n \rfloor^2 +\lfloor \sqrt n \rfloor\\ \bigr(-\frac{\lfloor \sqrt n \rfloor}{2}+(n-\lfloor \sqrt n \rfloor^2-\lfloor \sqrt n \rfloor), \frac{\lfloor \sqrt n \rfloor}{2} -\lfloor \sqrt n \rfloor \bigr), & \lfloor \sqrt n \rfloor^2 + \lfloor \sqrt n \rfloor + 1 \le n \lt (\lfloor \sqrt n \rfloor+1)^2\\\\ & \text{when } \lfloor \sqrt n \rfloor \text{ is odd:}\\ \bigr(\frac{\lfloor \sqrt n \rfloor+1}{2}, \frac{1-\lfloor \sqrt n \rfloor}{2} + (n-\lfloor \sqrt n \rfloor^2) \bigr), & \lfloor \sqrt n \rfloor^2 \le n \le \lfloor \sqrt n \rfloor^2 +\lfloor \sqrt n \rfloor\\ \bigr(\frac{\lfloor \sqrt n \rfloor+1}{2}-(n-\lfloor \sqrt n \rfloor^2-\lfloor \sqrt n \rfloor), \frac{1-\lfloor \sqrt n \rfloor}{2} + \lfloor \sqrt n \rfloor \bigr), & \lfloor \sqrt n \rfloor^2 + \lfloor \sqrt n \rfloor + 1 \le n \lt (\lfloor \sqrt n \rfloor+1)^2\\\\\end{cases} } $$