How do you create an alternating series with the sign being the same twice in a row?

If you are looking for a formula for the sequence $1,1,-1,-1,1,1,-1,-1,\ldots$ then try some of the suggestions at OEIS A057077, such as $$(-1)^{n(n-1)/2}$$


It seems that you want to simulate the series $1, 1, -1, -1, 1, 1, -1, -1, ...$. These kinds of alternating series are usually easiest to do using $\sin$ or $\cos$ functions. (Although the formula $(-1)^{n(n-1)/2}$ works just as well, I think it's kind of interesting to see this series modeled with a trig function, so I'm going to do it anyway.)

Think about a $\cos$ function going through $(0, 1)$, $(1, 1)$, $(2, -1)$, $(3, -1)$, $(4, 1)$ and so on.

Where are the maximums and minimums? They're in between points with the same values, like $\frac{0+1} 2=0.5$ and $\frac{2+3} 2=2.5$.

Which is the maximum? It's the one in between the points of higher value, which is $0.5$. This represents our right shift since $\cos 0$ is the maximum.

What's the period? It's two times the difference between two consecutive extrema, or $2(2.5-0.5)=4$. The period is usually $2\pi$, so our coefficient of $x$ must be $\frac{2\pi} 4=\frac \pi 2$.

Thus, we have $\cos\left(\frac \pi 2\left(x-\frac 1 2\right)\right)$. However, this gives us $\frac{1}{\sqrt 2}$ for $x=0$, so we must multiply by $\sqrt 2$ to get the following function:

$$f(x)=\sqrt 2\cos\left(\frac \pi 2\left(x-\frac 1 2\right)\right)$$

If we put this into WolframAlpha, we clearly see that this gives us the sequence we wanted.


There is a very general way to attack the problem of writing a repeating sequence of coefficients.

The discrete Fourier transform of the sequence "$1,1,-1,-1$", of length $N=4$, is $0,1+\mathrm{i},0,1-\mathrm{i}$". This says that $$ 0 \mathrm{e}^{-2\pi \mathrm{i} \cdot 0 n /N} + (1+\mathrm{i}) \mathrm{e}^{-2\pi \mathrm{i} \cdot 1 n /N} + 0 \mathrm{e}^{-2\pi \mathrm{i} \cdot 2 n /N} + (1 - \mathrm{i}) \mathrm{e}^{-2\pi \mathrm{i} \cdot 3 n /N} $$ is a constant multiple of your desired sequence. ("Constant multiple" because there are constant premultipliers I have ignored to make the above more direct.) That expression, in which we may ignore the two terms starting with zero, produces a sequence starting from $n=0$: $$ 2, 2, -2, -2, 2, 2, -2, -2, \dots \text{.} $$ So, $$ \frac{(1+\mathrm{i}) \mathrm{e}^{-2\pi \mathrm{i} \cdot 1 n /4} + (1 - \mathrm{i}) \mathrm{e}^{-2\pi \mathrm{i} \cdot 3 n /4}}{2} $$ does what you want.

Just as for $(-1)^n$, we may rewrite this using trigonometric functions. $$ \left(\cos \left(\frac{\pi n}{2}\right)-\sin \left(\frac{\pi n}{2}\right)\right) (\cos (\pi n)-i \sin (\pi n)) $$

Say, instead of starting at $n=0$, you want to start at $n=1$. We cyclically rotate the sequence to get the term we want at the position where $n=1$, i.e., we consider the sequence $-1,1,1,-1$. This gives transform $0,-1+\mathrm{i}, 0, -1-\mathrm{i}$. The term to use in your sum is then $$ \frac{(-1+\mathrm{i}) \mathrm{e}^{-2\pi \mathrm{i} \cdot 1 n /4} + (-1 - \mathrm{i}) \mathrm{e}^{-2\pi \mathrm{i} \cdot 3 n /4}}{2} \text{,} $$ which is equivalent to $$ \left(\sin \left(\frac{\pi n}{2}\right)+\cos \left(\frac{\pi n}{2}\right)\right) (-\cos (\pi n)+i \sin (\pi n)) \text{.} $$


Just for kicks:

$$f(n)=\lim_{x\to n_-}\left\lfloor{\sin \frac{\pi x}{2}}\right\rfloor$$

This can be used to generalize sequences like:

$$\underbrace{1,1,1,\cdots,1}_{k \space1's},\underbrace{-1,-1,-1,\cdots,-1}_{k \space 1's}, 1\cdots $$

Can be represented as

$$f(n)=\lim_{x\to n_-} \left\lfloor{\sin \frac{\pi x}{k}}\right\rfloor$$

I thought that this might a bit cool for the OP.