46

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?

beep-boop
  • 11,091
  • 10
  • 43
  • 69
Senseful
  • 803
  • 1
  • 8
  • 17
  • 10
    Note that a simple recursion formula isn't possible, but $x_0=1$, $x_1=0$ and $x_{n+2}=-x_n$ works. That's kind of silly, however. – Thomas Andrews Apr 11 '12 at 19:04
  • How about $x_n = (-1)^{(n - 3\lfloor \frac{n}{3} \rfloor) + 1 }(n - 2 \lfloor \frac{n}{2} \rfloor)$ (starting at $n = 1$)? – Daniel Freedman Apr 11 '12 at 19:18
  • 14
    What's wrong with $1,0,-1,0,1,0,-1,0,\dots$? – Michael Joyce Apr 11 '12 at 19:37
  • 2
    How is "simplest" defined in a non-subjective manner? Fewest operators? Shortest equation? Something else entirely? – zzzzBov Apr 11 '12 at 20:15
  • 3
    @MichaelJoyce: I need it as a function that I can input into a calculator. – Senseful Apr 11 '12 at 21:50
  • 1
    Any expression that uses $(-1)^x$ is trigonometry because $(-1)^x$ is another notation for $\cos(\pi x)+ i \sin (\pi x)$. I wonder if there any calculators that do not have trigonometry. – Anixx Apr 11 '12 at 23:44
  • 8
    Your expression can be simplified to $$a_n=\frac{\left(-1\right)^n+1}{2}\cdot \left(-1\right)^{\frac{n(n-1)}{2}}$$ a factorisation of Mariano Suárez-Alvarez's answer – Henry Apr 12 '12 at 00:05
  • Are you looking for a programming solution or a mathematical solution? If this is a mathematical solution then $1,0,-1,0,-1,\ldots$ should be fine; if this is a programming solution I think this verges on off-topic. – Asaf Karagila Apr 12 '12 at 01:16
  • I'm just curious, what possible reason do you have for this question? Why such limited conditions? – user160917 Apr 12 '12 at 16:19
  • 1
    @Senseful please upload a picture of the calculator that has powers, but no trigonometry, i'm curious :) (also: wtf, who uses calculators?) – kritzikratzi Apr 12 '12 at 19:04
  • 1
    @Senseful Your "update" reminds me of those fine prints on coupons and promotions: "Excludes: ..." And you learn about it after everything is done. – Vadim Apr 14 '12 at 17:25
  • @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. – Senseful Apr 16 '12 at 04:28

18 Answers18

72

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

Raymond Manzoni
  • 41,072
  • 5
  • 79
  • 133
  • Can you tell me what the |'s are? –  Apr 11 '12 at 20:15
  • 3
    @JoshuaDrake: the absolute value ( in C language it would be abs(x % 4-2)-1 ) – Raymond Manzoni Apr 11 '12 at 20:17
  • I thought so, but I must be misunderstanding, what happens for n = 3? –  Apr 11 '12 at 20:18
  • wow, never mind, I somehow skipped n = 2. –  Apr 11 '12 at 20:20
  • 4
    @JoshuaDrake: note that the formula even works for non integer values (it gives a 'triangle wave'). – Raymond Manzoni Apr 11 '12 at 20:23
  • 2
    You can see the graph of $f(x) = |x \bmod 4-2|-1$ here: http://www.wolframalpha.com/input/?i=y%3D%7C%28x+mod+4%29+-+2%7C+-+1 – Xenon Apr 11 '12 at 22:13
  • @Joshua: I think you are confused because you start counting from $n=1$, whereas Raymond is starting from $n=0$. His answer is great. – Raskolnikov Apr 12 '12 at 19:39
  • @Raskolnikov Actually, as I commented, I just forgot 2 as I was processing the series. I've already +1 this since it had no "trigonometry in disguise". –  Apr 12 '12 at 19:48
  • I call this the 'fold' function. If you are applying it in programming, the behavior across 0 for mod can vary, so it might need to be tweaked. –  Apr 12 '12 at 20:26
  • @RiverC: yes in C for example % won't work for 'mod' for n negative ($n$ was supposed non negative here). You may use $n mod 4= n-4\operatorname{floor}\left(\frac n4\right)$ instead thanks to the definition of [floor](http://www.cplusplus.com/reference/clibrary/cmath/floor). – Raymond Manzoni Apr 12 '12 at 21:06
  • 1
    @Xenon: Thanks for the graph! Let's add another possible solution : $\left(\dfrac {-4}{n+1}\right)$ using the definition of the [Kronecker symbol](http://en.wikipedia.org/wiki/Arithmetic_function) – Raymond Manzoni Apr 12 '12 at 21:09
  • 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. –  Apr 12 '12 at 21:10
67

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

hmakholm left over Monica
  • 276,945
  • 22
  • 401
  • 655
  • Beat me to it ... – Mark Bennet Apr 11 '12 at 19:03
  • 1
    The equivalences here are interesting. Note also that the recurrence form is a disguise for the $i^n$ form, and converted to the related homogeneous linear differential equation recreates the trig form. – Mark Bennet Apr 11 '12 at 19:21
  • 57
    [But what if...](http://i.imgur.com/6LUVH.png) – anon Apr 11 '12 at 19:44
  • Ask a silly question, get a silly answer. – Colonel Panic Apr 11 '12 at 23:18
  • 6
    @anon +1 That was hilarious and unexpected. That said, if I see more than one meme post a day on this site, I'm flagging 'em. –  Apr 12 '12 at 00:29
  • @anon Is that Harry Styles of One Direction? –  Apr 12 '12 at 00:35
  • 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. – Rick Decker Jul 31 '12 at 02:10
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}$$

Chris Eagle
  • 32,100
  • 2
  • 85
  • 106
33

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

Pedro
  • 116,339
  • 16
  • 202
  • 362
Vadim
  • 5,439
  • 1
  • 15
  • 22
25

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

Mariano Suárez-Álvarez
  • 126,294
  • 9
  • 223
  • 348
19

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

Vadim
  • 5,439
  • 1
  • 15
  • 22
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.

zyx
  • 34,340
  • 3
  • 43
  • 106
7

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

Clive Newstead
  • 62,292
  • 4
  • 91
  • 158
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$.

David Mitra
  • 71,555
  • 9
  • 126
  • 186
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.

Fixee
  • 11,199
  • 6
  • 39
  • 64
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.

Chavdar
  • 31
  • 2
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)}$$

Ronald
  • 1,333
  • 1
  • 9
  • 18
  • Any expression that uses $(-1)^x$ is trigonometry because $(-1)^x$ is another notation for $\cos(\pi x)+ i \sin (\pi x)$ – Anixx Apr 11 '12 at 23:41
  • 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. – itdoesntwork Apr 12 '12 at 13:35
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}$$

Jay
  • 183
  • 5
1

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

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

flanglet
  • 111
  • 1
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}$.

draks ...
  • 17,825
  • 7
  • 59
  • 171
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.

Almo
  • 111
  • 5
0

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.

Harry
  • 437
  • 2
  • 10
-1

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

2 - (((x + 1) | (x + x)) & 3)
sam hocevar
  • 556
  • 3
  • 10
  • Your solution is too complicated. The simplest solution no one has yet posted is mathematical expression `n%2`. Please see my answer for an explanation how it works. http://math.stackexchange.com/a/134935/20207 – bodacydo Apr 21 '12 at 19:43
  • 4
    @bodacydo - your solution is not a solution; it never outputs `-1`. – sam hocevar Apr 21 '12 at 21:12