Function which creates the sequence 1, 2, 3, 1, 2, 3, ... I was wondering how to map the set $\mathbb{Z}^+$ to the sequence $1, 2, 3, 1, 2, 3, \ldots$. I thought it would be easy, but I was only able to obtain an answer through trial and error.
For a function $f \colon \mathbb{Z}^+ \rightarrow \mathbb{Z}$, we have that
$f(x) = x \bmod 3$ gives the numbers $1, 2, 0, 1, 2, 0, \ldots$
$f(x) = (x \bmod 3) + 1$ gives the numbers $2, 3, 1, 2, 3, 1, \ldots$
After a bit of experimenting, I finally found that
$f(x) = ((x + 2) \bmod 3) + 1$ gives the numbers $1, 2, 3, 1, 2, 3, \ldots$
More generally, if I want to map the set $\mathbb{Z}^+$ to the sequence $\{1, 2, \ldots, n, 1, 2, \ldots, n, \ldots\}$, I need to use the function
$$f(x) = ((x + n - 1) \bmod n) + 1$$
I was only able to come to this result by trial and error. I was not able to find a solution to this relatively simple question online (although perhaps my search terms were off).
How would one come to this result in a more systematic way?

Let be $\omega\in\Bbb C$, $\omega^3 = 1$, $\omega\ne 1$. $\omega^n$ gives:
$$\omega^0 = 1,$$
$$\omega^1 = \omega,$$
$$\omega^2 = \omega^{-1},$$
$$\omega^3 = 1,$$
$$\cdots$$
"But I want $1,2,3,\dots$!" Solution: compose with a polynomial $P$ s.t.
$$P(1) = 1,\qquad P(\omega) = 2,\qquad P(\omega^{-1}) = 3.$$
Now,
$$P(\omega^0) = 1,$$
$$P(\omega^1) = 2,$$
$$P(\omega^2) = 3,$$
$$P(\omega^3) = 1,$$
$$\cdots$$
This trick will work for any periodic sequence.
