Getting the sequence $\{1, 0, -1, 0, 1, 0, -1, 0, \ldots\}$ without trig?
What's the simplest way to write a function that outputs the sequence:
{1, 0, -1, 0, 1, 0, -1, 0, ...}
... without using any trig functions?
I was able to come up with a very complex sequence involving -1 to some complicated formula, but I was hoping there is a more simple solution.
$n$ should start at 0 and go to infinity.
Update:
All the solutions you guys provided are great! I wasn't aware there were so many of them. I should have mentioned that I prefer a solution which doesn't use recursion; imaginary numbers; matrices; functions with if
statements; or functions such as ceil
, floor
, or mod
. I'm looking for something using basic algebra: addition/subtraction, multiplication/division, exponents, etc. However, I will accept anything since I didn't include this clause originally.
This is what I came up with:
$$a_n=\frac{\left(-1\right)^n+1}{2}\cdot \left(-1\right)^{\left(\frac{n}{2}-\frac{\left(-1\right)^{n+1}+1}{4}\right)}$$
Is there a less complicated way (i.e. fewer terms) to get this same sequence?
Solution 1:
Let's try too : $|n \bmod 4-2|-1$
Solution 2:
How about $\dfrac{i^n + (-i)^n}{2}$? (Of course, that is arguably just trigonometry in disguise).
Or as a recurrence: $a_n = -a_{n-2}$ with $(a_0,a_1)=(1,0)$.
Or $\begin{bmatrix}1 & 0\end{bmatrix}\begin{bmatrix}0&-1\\1&0\end{bmatrix}^n\begin{bmatrix}1\\0\end{bmatrix}$? (Which can be viewed as a better-disguised version of either of the two previous suggestions).
Solution 3:
Whether this is simplest will depend on exactly what you mean, but the following is a pretty simple description. It's certainly simpler than anything involving trig functions.
$$a_n=\begin{cases} 0 & \text{if n is odd} \\ 1 & \text{if n is divisible by 4} \\ -1 & \text{otherwise} \end{cases}$$
Solution 4:
What could possibly be easier than $\Re(i^n)$, $n=0,1,\ldots$?