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?

I'm surprised no one has mentioned composition. You've identified that you need to shift the range
$$f_1(x) = x - 1$$
then take the value modulo $n$
$$f_2(x) = x \mod n$$
and finally shift the result again
$$f_3(x) = x + 1$$
The function you want is just the composition $f = f_3 \circ f_2\circ f_1$.
