# Parametrizing the square spiral

Related to this question concerning number spirals I have another one, more specific.

While it is rather easy to arrange the natural numbers along an Archimedean spiral by

$$x(n) = \sqrt{n}\cos(2\pi\sqrt{n})$$ $$y(n) = \sqrt{n}\sin(2\pi\sqrt{n})$$

it's much more difficult to arrange them along a square spiral by a closed formula. It's very easy to construct the square spiral algorithmically ("go along straight lines and always turn to the right if possible") but I'm totally stuck how the functions $$x(n),y(n)$$ would look like as formulaic expressions. The only thing I'm quite sure about is that they will make use of the square root function - but how are the "radii" and the turns coded?

This is how the two spirals look like (the Archimedean spiral being appropriately scaled and rotated to align the square numbers):

And here you can see them being morphed into each other.

Can anyone give me a hint (or the solution)?

• I think it's useful to feed the numbers to the OEIS and see what you get. I did for the diagonal of the 1st quadrant (0,6,20,42,72,...) and the sequence on the positive y axis and found that the 1st is A002943, the second A007742. Both references use the word square spiral. You may want to look at it. I am not sure if you can get one parametrization for the whole square but I think every sequence will be listed. Commented Mar 22, 2019 at 12:12
• You should start here, at the Online Encyclopedia of Integer Sequences (OEIS) \oeis.org/A268038 Your square spiral is rotated $90^\circ$ from the one on OEIS. Commented Mar 22, 2019 at 13:50

I am not sure if this answers the question. We can note that the squares of even numbers are on the diagonal of the second quadrant, so if we set: $$\hat n=\max\{2k\mid (2k)^2\leqslant n\},$$ or in other words: $$\hat n=\left\{ \begin{array}{cl} \lfloor \sqrt n\rfloor & \mbox{if \lfloor \sqrt n\rfloor is even}\\ \lfloor \sqrt n\rfloor-1 & \mbox{if \lfloor \sqrt n\rfloor is odd} \end{array} \right.,$$ then we can easily arrange numbers in the integer lattice by the rule: $$(x(n),y(n))= \left\{ \begin{array}{cl} (-\frac{\hat n}{2}+n-\hat n^2,\frac{\hat n}{2}) & \mbox{if \hat n^2\leqslant n\leqslant\hat n^2+\hat n}\\ (\frac{\hat n}{2},\frac{\hat n}{2}-n+\hat n^2+\hat n) & \mbox{if \hat n^2+\hat n< n\leqslant\hat n^2+2\hat n+1}\\ (\frac{\hat n}{2}-n+\hat n^2+2\hat n+1,-\frac{\hat n}{2}-1) & \mbox{if \hat n^2+2\hat n+1< n\leqslant\hat n^2+3\hat n+2}\\ (-\frac{\hat n}{2}-1,-\frac{\hat n}{2}-1+n-\hat n^2-3\hat n-2) & \mbox{if \hat n^2+3\hat n+2< n\leqslant\hat n^2+4\hat n+3} \end{array} \right..$$

• Please have a look at my answer below. Commented Mar 25, 2019 at 16:33
• You may want to have a look at this follow-up question. Commented Mar 26, 2019 at 11:06
• @Hans-PeterStricker Very interesting.
– SMM
Commented Mar 26, 2019 at 11:51

Consider "piecewise linear approximations" of the sine and cosine function, periodically defined on the unit interval, i.e. $$x \in [0,1]$$.

Let

$$\boxed{\cos_\bigcirc(x) = \cos(2\pi x)\\\sin_\bigcirc(x) = \sin(2\pi x)}$$

and compare this to

$$\boxed{\cos_\square(x) = \begin{cases} +1 & \text{ for } \frac{0}{8} \leq x \leq \frac{1}{8} \\ +2 - 8x & \text{ for } \frac{1}{8} \leq x \leq \frac{3}{8} \\ -1 & \text{ for } \frac{3}{8} \leq x \leq \frac{5}{8} \\ -6 + 8x & \text{ for } \frac{5}{8} \leq x \leq \frac{7}{8} \\ +1 & \text{ for } \frac{7}{8} \leq x \leq \frac{8}{8} \\ \end{cases} \\ \\\sin_\square(x) = \begin{cases} +0 + 8x & \text{ for } \frac{0}{8} \leq x \leq \frac{1}{8} \\ +1 & \text{ for } \frac{1}{8} \leq x \leq \frac{3}{8} \\ +4 - 8x & \text{ for } \frac{3}{8} \leq x \leq \frac{5}{8} \\ -1 & \text{ for } \frac{5}{8} \leq x \leq \frac{7}{8} \\ -8 + 8x & \text{ for } \frac{7}{8} \leq x \leq \frac{8}{8} \\ \end{cases}}$$

$$\cos_\square(x) = \begin{cases} +1 & \text{ for } 0 \leq 8x \leq 1 \\ +2 - 8x & \text{ for } 1 \leq 8x \leq 3 \\ -1 & \text{ for } 3 \leq 8x \leq 5 \\ -6 + 8x & \text{ for } 5 \leq 8x \leq7 \\ +1 & \text{ for }7 \leq 8x \leq 8 \\ \end{cases} \\ \\\sin_\square(x) = \begin{cases} +0 + 8x & \text{ for } 0 \leq 8x \leq 1 \\ +1 & \text{ for } 1 \leq 8x \leq 3 \\ +4 - 8x & \text{ for } 3 \leq 8x \leq 5 \\ -1 & \text{ for } 5 \leq 8x \leq7 \\ -8 + 8x & \text{ for }7 \leq 8x \leq 8 \\ \end{cases}$$

These are the plots:

$$\cos_\square$$ and $$\sin_\square$$ are especially well suited to arrange numbers on a square with integer coordinates around the origin with uniform distance $$1$$ along the square.

This only works for multiples of $$8$$ with $$8n = (2n+1)^2 - (2n-1)^2$$. In this case the positions of the $$8n$$ numbers $$k = 0, 1, \dots, 8n-1$$ are given by

$$\boxed{x^{(n)}_\square(k) = n\cos_\square(\frac{k}{8n})\\ \\y^{(n)}_\square(k) = n\sin_\square(\frac{k}{8n})}$$

Compare this to the positions of $$8n$$ numbers on a circle around the origin with uniform distance $$\frac{2\pi}{8}$$ along the circle:

$$\boxed{x^{(n)}_\bigcirc(k) = n\cos_\bigcirc(\frac{k}{8n})\\ y^{(n)}_\bigcirc(k) = n\sin_\bigcirc(\frac{k}{8n})}$$

For the (circular) Archimedean spiral we have

$$x_\bigcirc(k) = -\frac{\sqrt{k}}{2}\cos_\bigcirc(\frac{\sqrt{k}}{2}-\frac{1}{8})$$ $$y_\bigcirc(k) = -\frac{\sqrt{k}}{2}\sin_\bigcirc(\frac{\sqrt{k}}{2}-\frac{1}{8})$$

Written for the sake of comparison with the square spiral:

$$\boxed{x_\bigcirc(k) = - x_\bigcirc^{(\sqrt{k}/2)}(2k-\frac{1}{8})\\ y_\bigcirc(k) = -y_\bigcirc^{(\sqrt{k}/2)}(2k-\frac{1}{8})}$$

Note that the factors $$-1$$, $$\frac{1}{2}$$ and the phase $$\frac{1}{8}$$ (which corresponds to $$\frac{\pi}{4}$$) where chosen to align the Archimedean with the square spiral, especially the square numbers.

The formulas

$$x_\bigcirc(k) = \sqrt{k}\cos_\bigcirc(\sqrt{k})$$ $$y_\bigcirc(k) = \sqrt{k}\sin_\bigcirc(\sqrt{k})$$

would give an Archimedean spiral as well.

This is for the square spiral. Let $$k'$$ be the greatest odd perfect square smaller than $$k$$. Let $$\hat{k} = (\sqrt{k'}-1)/2$$. Let $$x_\square(0) = 0$$ and $$y_\square(0) = 0$$ and for $$k > 0$$

$$\boxed{x_\square(k) = x_\square^{(\hat k)}(k - k' - \hat k + 1) \\ y_\square(k) = y_\square^{(\hat k)}(k - k' - \hat k + 1)}$$

Note that $$k - k' - \hat k + 1$$ being negative doesn't pose a problem since $$\cos_\square$$ and $$\sin_\square$$ are periodic in both directions.

By adapting the formula found at A174344 to be non-recursive using summation notation, you may get the following:

$$x(n) = \sum_{k=1}^{n} \sin(\frac{\pi}{2}\left \lfloor \sqrt{4k-3} \right \rfloor)$$

$$y(n) = \sum_{k=1}^{n} \cos(\frac{\pi}{2}\left \lfloor \sqrt{4k-3} \right \rfloor)$$

You can see this in action here on Desmos.

$$(x(n),y(n))$$ generates a clockwise square spiral beginning in the $$+x$$ direction. By negating one or both and/or swapping $$x(n)$$ and $$y(n)$$, you can create different orientations of the square spiral. The orientation in your example and on the Desmos graph use $$(x(n),-y(n))$$.