Generate the list [1, 0, -1, 0, 1, 0, -1, 0, ...] [closed]

In the spirit of a question on math.se: given an integer n, return the integer in position n of the sequence [1, 0, -1, 0, 1, 0, -1, 0, ...]. Assume n >= 0.

Rules:

1. You may only use the operations -x, x+y, x-y, x*y, x/y, and xy, whatever their representations are in the language you choose.
2. Your function must be called a.
3. Your function must run in constant time for all n.

Example:

a=lambda n:((-1)**(n*n-n)//2)+(-1)**(n*n+n)//2))//2


Non-example (uses list subscripting operation and modulo operator):

a=lambda n:[1,0,-1,0][n%4]


Non-example (uses cos operation):

from math import *;a=lambda n:int(cos(n*pi/2))


Non-example (does not run in constant time and uses if operation):

a=lambda n:-a(n-2) if n>1 else 0 if n>0 else 1


Clarifications: Both integer and float division are allowed. Your solution may return either an integer or a float (or complex number, I suppose, since some answers already used it). If your solution returns a float: your solution should, if it were to theoretically be carried out to infinite precision, give the exact answer.

• Is the allowed x/y any kind of division (including div) or always floating point division? — Certainly, "Your function must run in constant time for all n" is limited to some reasonable limit, no need to work for bigInts? Apr 15 '12 at 15:05
• What about conditionals, Boolean negation, etc? Apr 15 '12 at 22:07
• Your example uses ** and // but you didn't allow them. a) What does ** and // mean, and why can you use them? Apr 15 '12 at 23:25
• How does the restriction on the name of the function work in languages where functions can't be named, or where a isn't a valid function name?
– user62131
Dec 19 '16 at 22:07
• @HatWizard in little endian. ... not a bad point. Jun 7 '18 at 2:54

Ruby, 26 characters

a=->n{1-(k=n-n/4*4)+k/3*2}


Example:

> p (0..10).map{|n| a[n]}
[1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1]


Note: This solution assumes that integer division / is an allowed operation.

• I know it's been 6 years (lol..), but it can be golfed by 1 byte by reusing n instead of using a new variable k: a=->n{1-(n-=n/4*4)+n/3*2}. Try it online 25 bytes. Apr 12 '18 at 12:53

C, 36 chars

a(n){return(1-n+n/2*2)*(1-n+n/4*4);}


The same function used by Peter Taylor.

• By using a(n){return(n-n/2*2)*(2-n+n/4*4);} you can shave off two characters. May 2 '12 at 22:10
• @Superbest, this gives the series shifted by one position. Close, but not it. May 3 '12 at 7:49
• According to codepad.org/sO0mFkhI output from your function is 0, -1, 0, 1, 0, -1, 0, 1, 0, .... Output from mine is 1, 0, -1, 0, 1, 0, -1, 0, 1. The original question gives the example output 1, 0, -1, 0, 1, 0, -1, 0, .... May 3 '12 at 9:57
• @Superbest, you seem to start with n=1, I start with n=0. All examples (except the first, which gives a syntax error), give a(0)=1, like my answer. You can say your suggestion is valid, because it gives the same series starting from another position, but I'll stick with my version. May 3 '12 at 13:00
• Ah, that's right. I'm not sure why I thought the first one was 1 not 0. Sorry. May 3 '12 at 13:14

Python, 29 chars

a=lambda n:(1j**n+(-1j)**n)/2


Kind of cheating, but fun.

Edit:

Now that we can return complex numbers, this is competitive and no longer cheating (with Howard's suggestion).

• I think that operator .real is not supported by the rules. You may use (j**n+(-j)**n)/2 instead. Apr 16 '12 at 4:04
• Should be 1j**-n. Aug 8 '15 at 4:31
• 1j**-n instead of (-1j)**n saves 2 bytes. Mar 30 '18 at 14:35

Python, 36 chars

a=lambda n:(-1)**(n/2)*(1+(-1)**n)/2


J, 18 characters

a=:[:{.[:+._1^2%~]


Works, but is probably cheating by the rules set out above.

Calculates -1 to the power of the given integer divided by 2 which gives 0j1 for any odd number. The {. and the +. return the real part so that the odd numbers give 0.

Usage:

   a 1
0
a 6
_1


Edit: After feeling a bit smug about this I've just looked properly at Keith's answers and realized I've done essentially the same thing.

GolfScript (20 19 chars)

{.4/4*-.3/2*)\-}:a;


This is a port of Howard's solution.

If, in addition to division, the remainder operation were permitted, this could be improved substantially (by 6 5 chars):

{.2%(\4%(*}:b;


Mathematically that's

x => (x%2 - 1) (x%4 - 1)


I can't see any logic behind excluding remainder, since it's defined in terms of permitted operations.

• I think you should spend a semicolon after your code. Otherwise it interferes with the input to your program. Apr 16 '12 at 11:06
• @Howard, I see your point. I think it's arguable, because the spec's badly worded, but on balance I think the leave-it-off position is weaker. Apr 16 '12 at 11:14

Python, 34 chars

Poor man's modulus... saves a whopping 2 chars over Keith Randall's solution. Take that! ;)

a=lambda n:(n-1-n/2*2)*(n-1-n/4*4)


edit: on reading others' solutions, this is the same as Peter Taylor's

bc: 28 chars

echo 'a=4;(1-a+a/2*2)*(1-a+a/4*4)' | bc


Python, 49 chars

a=lambda n:(lambda x:(x**3-3*x*x-x+3)/3)(n-n/4*4)


Note: In Python3 you'll have to replace n/4 with n//4 at the end.

8086 Assembler (14 bytes of machine code):

 ; ax holds the index into the sequence
; returns the sequence value in ax
a: inc al
shl al,6
sar al,6
cbw
xor al,ah
cbw
ret

• But in machine code, it isn't named a. The name's only used by the assembler and by the linker, so you'd have to count the length of the assembly code or the object code (unless the OP clarifies that it's ok for the function to be named 0 instead, which would be the closest analogue to the name of a function in machine code).
– user62131
Dec 19 '16 at 22:10

TI-Basic, 12 bytes

i^Ans+i^-Ans
Ans/2


Store in prgmA and run with 5:prgmA, with the input in place of 5.

TI-Basic implicitly returns the last evaluated value.

Python 2, 28 bytes

a=lambda n:(n/2*2-n+1)*1j**n


Try it online!

Proof of validity for those who don't know Python:

The expression is (n/2*2-n+1)*1j**n. If you add parentheses, it becomes ((((n/2)*2)-n)+1)*(1j**n). / is integer division, since both n and 2 are integers in this case, * is multiplication, - is subtraction, + is addition and ** is exponentiation.

Java 8, 21 bytes

n->1-(n-=n/4*4)+n/3*2