# #38: Spirals

posted on 2018-03-20

Recall that the symbol $\mathbb{Z}^2$ denotes the set of all coordinate pairs with integer components.

A spiral of length $n\ge 1$ is a counter-clockwise spiral starting at $(0,0)$ extending to the right to $(n,0)$, wrapping around so as to cover $\mathbb{Z}^2$ completely.

Consider the following spiral of length $2$.

*--*--*--*--*--*
|              |
*  *--*--*--*  *
|  | (0,0)  |  |
*  *  x--*--x  *
|  |     (2,0) |
*  *--*--*--*--*
|
*--*--*--*--*--* ...

The points of the spiral have a natural ordering. The above spiral can be written as a sequence $s$ where \begin{align} s_0 &= (0,0), & s_3 &= (2,1),\\ s_1 &= (1,0), & s_4 &= (1,1),\\ s_2 &= (2,0), & s_5 &= (0,1), \end{align} and so on.

There are three challenges. Given an $n\ge 1$, do the following.

1. Given a $k\ge 0$, find $s_k$.

2. Given a point $(\alpha, \beta)\in\mathbb{Z}^2$, find the $k$ such that $s_k = (\alpha,\beta)$.

3. Print the spiral of length $2$ to the terminal by printing the values of $k$ in their correct positions. (And make it look nice by ensuring the numbers are aligned and reasonably spaced out.)

This blog covers math, challenge

View content from 2018-01, 2018-02, 2018-03