# How to generate an oscillating integer sequence without trig functions?

I would like a function of t, where t is an integer increasing over time, that an average person could compute in their head that results in a sequential, oscillating sequence of integers. For example:

0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3...

I have a function that almost computes what I want using sine (it doubles up on the peaks and valleys), but given a value for t, most humans cannot compute sine in their head.

x = floor(4*sin(t/4 + 5.5) + 1

I've been playing around with mod operation. Mod nicely builds a cycle, obviously, but I haven't been able to figure out a modification to avoid the discontinuity -- basically, I want to reverse every odd sequence of the mod cycle.

Is there a function that satisfies my requirements? Duplicating the peaks and valleys is acceptable, but I'd prefer to avoid that. Bonus points if you can express the function in terms of min and max, the top and bottom integers of the cycle!

Assuming $$t$$ starts at $$0$$ (to start $$t$$ at $$1$$ instead, just replace $$t$$ with $$t - 1$$ below), then for any positive integer $$n$$, the cycles from $$0$$ to $$n$$ and back to $$1$$ before starting again at $$0$$ can be obtained using

$$f(t) = t\left(-1\right)^{\left\lfloor t/n \right\rfloor} \bmod{2n} \tag{1}\label{eq1A}$$

where $$\bmod{2n}$$ means the remainder $$0 \le r \le 2n - 1$$ when the result is divided by $$2n$$.

Your specific example sequence has $$n = 7$$, so it can be derived from

$$f(t) = t\left(-1\right)^{\left\lfloor t/7 \right\rfloor} \bmod{14} \tag{2}\label{eq2A}$$

Note \eqref{eq1A} works for $$0 \le t \le n - 1$$ because $$\left\lfloor\frac{t}{n}\right\rfloor = 0$$, giving $$(-1)^{0} = 1$$ so $$f(t) = t$$. For $$n \le t \le 2n - 1$$, we have $$\left\lfloor\frac{t}{n}\right\rfloor = 1$$, giving $$(-1)^{1} = -1$$ so $$f(t) = 2n - t$$, e.g., $$f(n) = n, f(n + 1) = n - 1, \; \ldots \; , f(2n - 1) = 1$$. With \eqref{eq2A}, this gives $$f(7) = 7, f(8) = 6, \; \ldots \; , f(13) = 1$$.

For $$2n \le t \le 3n - 1$$, then $$\left\lfloor\frac{t}{n}\right\rfloor = 2$$, giving $$(-1)^2 = 1$$, so since $$t \bmod{2n} = t - 2n$$, then $$f(t) = t - 2n$$ with $$f(2n) = 0, f(2n + 1) = 1, \; \ldots \; , f(3n - 1) = n - 1$$, i.e., the cycle is now repeating. Continuing with increasing $$t$$ gives the rest of the sequence you are asking for.

• That does compute. I'll leave the window open for a bit in case someone has a simpler function, but this will work. Thank you. – SRM Dec 9 '20 at 23:40