44
$\begingroup$

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?

  • 0
    @Vadim'vcherep': I wasn't aware there were so many ways to do this (all of which are great), otherwise I would have included the clause earlier. However, I should have used the word *prefer*, because all these options are valid, as you implied.2012-04-16

17 Answers 17

71

Let's try too : $|n \bmod 4-2|-1$

  • 0
    This function is extremely useful in animation where you need cylical motion at say, 60 frames a second, but don't want the overhead of the circle functions and don't care about the bumpiness.2012-04-12
66

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).

  • 1
    @rahul. Count yourself lucky that you don't frequent scifi.SE, where the mother of all meme sites is occasionally referenced, namely tvtropes.org. Over there, any reference to that site usually contains a SPOILER tag, since going there is essentially equivalent to falling into a black hole, time-waste-wise.2012-07-31
39

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}$

  • 2
    This is so brutally simple I never ever were able to find it.2012-04-13
33

What could possibly be easier than $\Re(i^n)$, $n=0,1,\ldots$?

  • 0
    totally what I wanted to suggest as well2012-04-12
25

$\frac{1}{2} \left((-1)^{(n-1) n/2}+(-1)^{n (n+1)/2}\right)$

  • 2
    @Anixx, your last comment is an egregius example of a non-sequitur!2012-07-30
18

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).

  • 0
    Great answer. I hope it is not underrated.2012-04-11
17

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.

7

My favourite is $(-1)^{\frac{n-1}{2}}(n \text{ mod } 2 )$. Seems quite tidy compared to other possible expressions.

4

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$.

4

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.

  • 2
    If `n` is an `unsigned` it'll keep on working past integer overflow, too.2012-04-12
3

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.

  • 0
    This can be made more symmetrical as $(1 - n\bmod 4)(1 - n \bmod 2)$2012-04-16
1

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)}$

  • 4
    @Anixx he needs to be able to enter it into a calculator, presumably without trigonometric functions. Most calculators can evaluate exponents, this should be fine.2012-04-12
1

$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}$

1

With binary operators (not the prettiest solution but probably the fastest):

((n&1)^1) - ((n&2) & ~((n&1)<<1))

  • 0
    I love stuff like this2012-04-24
1

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}$.

  • 0
    @PeterTamaroff ahh, yeah I like([use](http://math.stackexchange.com/a/107169/19341)) this kind. But what do you mean with $[x^n]$?2012-07-30
0

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.

-1

Here is a solution using only 5 bitwise or arithmetic operations:

2 - (((x + 1) | (x + x)) & 3) 
  • 3
    @bodacydo - your solution is not a solution; it never outputs `-1`.2012-04-21