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?
$\endgroup$ 1218 Answers
$\begingroup$Let's try too : $|n \bmod 4-2|-1$
$\endgroup$ 12 $\begingroup$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).
$\endgroup$ 7 $\begingroup$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}$$
$\endgroup$ 1 $\begingroup$What could possibly be easier than $\Re(i^n)$, $n=0,1,\ldots$?
$\endgroup$ 1 $\begingroup$$$\frac{1}{2} \left((-1)^{(n-1) n/2}+(-1)^{n (n+1)/2}\right)$$
$\endgroup$ 7 $\begingroup$If you have a sequence and have a linear recursion formula generating the sequence, then you can easily transform it into a closed form solution using one of many methods available.
In your case let us start with the simplest recursion: $a_0=1,a_1=0,a_n=-a_{n-2},n\ge 2$. The easiest (if you have access to a computer) way to obtain a closed form solution is to use matrix theory.
First, we rewrite the recursion in matrix form: $\left(\matrix{ a_n \\ a_{n-1} }\right)=\left(\matrix{ 0 & -1 \\ 1 & 0 }\right)\left(\matrix{ a_{n-1} \\ a_{n-2} }\right)$.
From here we get: $$\left(\matrix{ a_n \\ a_{n-1} }\right)=\left(\matrix{ 0 & -1 \\ 1 & 0 }\right)^{n-1}\left(\matrix{ a_1 \\ a_0 }\right)=\left(\matrix{ 0 & -1 \\ 1 & 0 }\right)^{n-1}\left(\matrix{ 0 \\ 1 }\right)$$ A standard method to proceed at this point is to diagonalize the matrix (here the computer is your best friend, for example: use Wolfram Alpha): $$\left(\matrix{ 0 & -1 \\ 1 & 0 }\right)=\left(\matrix{ -i & i \\ 1 & 1 }\right)\left(\matrix{ -i & 0 \\ 0 & i }\right)\left(\matrix{ -i & i \\ 1 & 1 }\right)^{-1}$$ $$\left(\matrix{ a_n \\ a_{n-1} }\right)=\left(\matrix{ -i & i \\ 1 & 1 }\right)\left(\matrix{ (-i)^{n-1} & 0 \\ 0 & i^{n-1} }\right)\left(\matrix{ i/2 & 1/2 \\ -i/2 & 1/2 }\right)\left(\matrix{ 0 \\ 1 }\right)$$ and $$a_n=\frac{1}{2}(-i)^n+\frac{1}{2}i^n$$
We could start from another form of recursion: for example, $a_0=1, a_1=0, a_2=-1$ and $a_n=-(a_{n-3}+a_{n-2}+a_{n-1})$, $n\ge 3$. Then write down a 3x3 matrix representing the recursion, diagonalize it, and obtain... well, the same solution. So, it does not matter how you represent the linear recursion: in general, you are going to obtain the same closed form solution.
Another way to find a closed form solution is to use generating functions. Using generating functions is easier to do by hand (no need to diagonalize and invert matrices).
$\endgroup$ 1 $\begingroup$The specific sequence is not essential. You are asking how to construct a function with period 4. Linear combinations of shifts of one $n$-periodic function can be used to write down any other $n$-periodic function, so they are all equally good in that sense. The trouble is to get at a sequence with period 4 without basing it on another one already known to have that period, such as $i^n$.
The answer is division by 2 which, as you can see, appears in all the answers that are not built from a $\mod 4$ operation.
If you are allowed as a primitive one integer function $f(n)$ of period 2, such as $(-1)^n$, and division by 2, then any function of order $2^k$ can be constructed.
From $f(n)$ and possible divisions by 2, one can construct the parity function $p(n) = n \mod 2$. Using that and more divisions by two, one can construct for each $i$ the function that equals the $i$'th binary digit of $n$.
I don't think there is a way to avoid as ingredients in the formula at least one 2-periodic function given as a primitive, and division by 2. Except by providing a 4-periodic operation at the start, in which case linear combinations are enough.
$\endgroup$ $\begingroup$My favourite is $(-1)^{\frac{n-1}{2}}(n \text{ mod } 2 )$. Seems quite tidy compared to other possible expressions.
$\endgroup$ $\begingroup$This may be silly, but ${(-1)^{\lceil n/2\rceil+1} }\cdot{(-1)^{n+1}+1\over 2}$, where $\lceil m\rceil$ is the smallest integer greater than or equal to $m$.
$\endgroup$ $\begingroup$If you like the programming language C, you could write
2*!(n % 4) - !(n % 2)which works for n from 0 up to the point where n overflows your integer size.
I think that |n mod 4−2|−1 is a great solution. Here is some that not require absolute value:
(1 - (n mod 4))((n+1) mod 2)
The logic behind it is: n mod 2 gives 0, 1, 0, 1.. because we want all odd numbers to be 0 we use n+1 mod 2. Than we use (1 - (n mod 4)) to make 0 input to output 1 and 2 input to output -1. n mod 4 is just to limit the numbers between 0 and 3.
Cheers.
$\endgroup$ 1 $\begingroup$If you wish to avoid imaginary numbers, you need to ensure that $-1$ takes an integer power: so your answer seems pretty efficient in that respect.
If we relax the condition of avoiding imaginary numbers, then we no longer require integer powers, because we can allow $i = (-1)^{\frac{1}{2}}$ as in several other answers.
Alternatively, if we allow clock arithmetic/modular arithmetic, or rounding with ceiling or floor, it becomes much more straightforward to engineer a discrete, repeating pattern.
Otherwise, I can simplify your answer just a little (require fewer operations):
$$a_n=\frac{\left(-1\right)^n+1}{2}\cdot \left(-1\right)^{\left(\frac{2n-1+\left(-1\right)^{n}}{4}\right)}$$
$\endgroup$ 2 $\begingroup$$$a_n=\frac{(-1)^n+1}{2}(-1)^{\frac{(-1)^n-1}{2}}$$
But, technically that's using imaginary because it is really doing $$a_n=\frac{(-1)^n+1}{2}i^{(-1)^n-1}$$
$\endgroup$ $\begingroup$With binary operators (not the prettiest solution but probably the fastest):
((n&1)^1) - ((n&2) & ~((n&1)<<1))
$\endgroup$ 3 $\begingroup$Let $a_n$ be defined like the coefficients of the series expansion at $x=0$ for $$ \frac1{1-x^2}=1-x^2+x^4-x^6\cdots=\sum\limits_{n=0}^\infty a_n x^n . $$ You'll get your sequence by calculating: $\displaystyle \;\;\;\left[\frac1{n!}\frac{d^n}{dx^n}\frac1{1-x^2}\right]_{x=0}$.
$\endgroup$ 4 $\begingroup$I like
function GetNumInSquence(int num)
{ assert(num >= 0); int m_lookup[4] = { 1, 0, -1, 0 }; int numInSquence = m_lookup[num % 4]; return numInSequence;
}No ifs or trig or anything; just a simple lookup. It's also easy to read the code. Someone else's 2*!(n % 4) - !(n % 2) is really cool, but it requires explanation to know what it's doing.
In binary (twos compliment), $a_n$ is the following two-bit number:$$(n_1 \cdot\bar{n}_0) \ \ \ \ (\bar{n}_0)$$
where $n_1$ and $n_0$ are the two least significant bits of $n$, $\bar{x}$ denotes "NOT $x$" and $\cdot$ denotes the boolean "AND" operator.
That's how it would be done in some picoseconds using a handful of transistors. This would be written as a function in a hardware description language, which is then synthesized into digital hardware.
$\endgroup$ $\begingroup$Here is a solution using only 5 bitwise or arithmetic operations:
2 - (((x + 1) | (x + x)) & 3) $\endgroup$ 2