# Formula defining specific sequences

Prelude

I'm trying to define a formula which will assist in the generation of some unusual numeric sequences. The sequences are known ahead of time, so I'm really just looking for a simple arithmetic formula that can be used to calculate them, rather than hard coding the values (so that it's more extensible for future updates to my system). I've tried this a number of ways but am not sure what method to use in order to attempt to define this. Any assistance would be extremely appreciated!

Known Values

when n = 1 the sequence = [0,2,...]

when n = 2 the sequence = [0,-1,3,2,...]

when n = 3 the sequence = [0,-1,-2,4,3,2,...]

when n = 4 the sequence = [0,-1,-2,-3,5,4,3,2,...]

when n = 6 the sequence = [0,-1,-2,-3,-4,-5,7,6,5,4,3,2,...]

when n = 8 the sequence = [0,-1,-2,-3,-4,-5,-6,-7,9,8,7,6,5,4,3,2,...]

All sequences loop on the "..." (i.e. when n=1 the sequence is really [0,2,0,2,0,2,etc.])

The task is to define a formula that defines the value of the ith member of the sequence, with prior-knowledge of the value of n.

aka x in f(n,i) = x

Fwiw, this is a real world problem to assist in the generation of dynamic imposition templates for a printing application :)

• So when n=1, there are only 2 terms in the sequence which keep on repeating? – Manish Kundu Jan 9 '18 at 2:45
• @ManishKundu infinite terms, repeated as 0,2,0,2,0,2... etc – John Doe Jan 9 '18 at 2:46
• It is $i -(2n+1) \lfloor \frac{i+n+1}{ 2n+1} \rfloor$ – user798409 Jan 9 '18 at 2:47

We can see that it is something like $$f(n,i)=\begin{cases}1-i&0<i\le n\\2n+2-i&n<i\le2n\end{cases}$$Then the repetition can be dealt with by modular arithmetic (or ceiling/floor functions). Suppose $i>2n$. Then computing $i\mod{2n}$ can be done by $$i\equiv i-2n\left\lfloor\frac{i-1}{2n}\right\rfloor\mod{2n}$$This will be used in place of $i$ in the final formula.
If $i\in[1,n]\mod{2n}$, then $$\frac {i}n\in(2k,2k+1]\implies\left\lceil\frac{i}{n}\right\rceil=2k+1$$and similarly, if $i\in[n+1,2n]\mod{2n}$, then $$\left\lceil\frac{i}{n}\right\rceil=2k$$
So we get $$f(n,i)=\frac{1-(-1)^{\left\lceil\frac{i}{n}\right\rceil}}{2}\left(1-i+2n\left\lfloor\frac {i-1}{2n}\right\rfloor\right)+\frac{1+(-1)^{\left\lceil\frac{i}{n}\right\rceil}}{2}\left(2n+2-i+2n\left\lfloor\frac {i-1}{2n}\right\rfloor\right)$$where the initial fractions are to choose whether we are in the range $i\in[1,n]$ or $i\in[n+1,2n]$ in$\mod{2n}$.
• @SJB If $i=n=1$, then $\left\lceil\frac in\right\rceil=1$, and $\left\lfloor\frac i{2n}\right\rfloor=0$, so you should get the first entry to be $0$, as required: $$f(1,1)=\frac{1-(-1)^1}{2}(1-1+2\cdot 0)+\frac{1+(-1)^1}{2}(2+2-1)=0$$ But yes, you're right it was slightly wrong, since it got the $2n$-th term wrong before. I believe it is fixed now. Before, it was giving a sequence [0,4,0,4,...]. Now, I believe the next term is $$f(1,2)=0\cdot(1-2+2)+\frac{1+1}{2}(2+2-2)=2$$(By the way, I am using that the first entry in the sequence has $i=1$, not $i=0$, which some people might use) – John Doe Jan 9 '18 at 3:50