Oscillating function with periodic change of sign and substraction of values

52 Views Asked by At

I'm pretty close defining a function that meets my criteria for oscillation of set of integer values. But I'm missing something that you can probably easily fix.

$$f(n) = (-1)^{\lfloor\frac{n}{6}\rfloor}\times(n-6\times\lfloor\frac{n}{6}\rfloor)$$

Integer values are:

0, 1, 2, 3, 4, 5, 0, -1, -2, -3, -4, -5, 0, 1, 2, 3, 4, 5

But should be:

0, 1, 2, 3, 4, 5, -1, -2, -3, -4, -5, -6, 6, 7, 8, 9, 10, 11, -7, -8, -9, -10, -11, -12, 12,...

Can you help to find out the correct formula to solve the situation?

Also this is quite near, but not exactly what I want:

$$f(n) = (-1)^{\lfloor\frac{n}{6}\rfloor}\times(n-6\times\lfloor\frac{n}{12}\rfloor)$$

0, 1, 2, 3, 4, 5, -6, -7, -8, -9, -10, -11, 6, 7, 8, 9, 10, 11,...

Note, that I rather not have if-else clauses, but do this purely as an algebraic function.

Another closer, yet uglier solution is to add sign oscillator to two other places on the equation:

$$f(n) = (-1)^{\lfloor\frac{n}{6}\rfloor}\times(n-6(-1)^{\lfloor\frac{n}{6}\rfloor}\times\lfloor\frac{n(-1)^{\lfloor\frac{n}{6}\rfloor}}{12}\rfloor)$$

This time giving:

0, 1, 2, 3, 4, 5, 0, -1, -2, -3, -4, -5, 6, 7, 8, 9, 10, 11,...