# Which function describes the following relation?

Supplementing my previous question I decided to ask a new one which is more general.

So is given a natural even number $k$ and a function $f_k:\mathbb{N}_0\rightarrow\mathbb{N}_0:n\rightarrowtail(n \;\text{mod} \; k) \rightarrowtail \text{miracle} \rightarrowtail f_k(n)$ such that:

Here comes the miracle explanation (how functions acts):

• $k = 2$

0 -> 0
1 -> 0

• $k = 4$

0 -> 0
1 -> 1
2 -> 1
3 -> 0

• $k = 6$

0 -> 0
1 -> 1
2 -> 2
3 -> 2
4 -> 1
5 -> 0

• $k = 8$

0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 3
5 -> 2
6 -> 1
7 -> 0


If $k$ would be odd, it would be simple: $$f(n) = |\frac{k}{2} - |n - \frac{k}{2}|\;|$$ (fraction without decimal part). But for even $k$'s I can't figure out something, so could you please help me to express this function (must be something with modulo, absolute value, Gauss brackets of kind of this).

Just use $(k-1)/2$ instead of $k/2$: $$f(n)=\Bigg\lvert\frac{k-1}2-\bigg\lvert n-\frac{k-1}2\bigg\rvert\Bigg\rvert.$$