5
1
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:
- 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.
- Your function must be called
a
. - 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 (includingdiv
) or always floating point division? — Certainly, "Your function must run in constant time for alln
" is limited to some reasonable limit, no need to work for bigInts? – ceased to turn counterclockwis – 2012-04-15T15:05:30.403What about conditionals, Boolean negation, etc? – Peter Taylor – 2012-04-15T22:07:53.167
Your example uses ** and // but you didn't allow them. a) What does ** and // mean, and why can you use them? – user unknown – 2012-04-15T23:25:54.740
@user unknown: Sorry I didn't make that clear.
x**y
is x to the yth power andx//y
is integer division. – Snowball – 2012-04-16T00:05:10.637@Peter Taylor: Conditionals, boolean negation, and such are not allowed. My real goal here was to translate that math problem I linked into a code golf problem. – Snowball – 2012-04-16T00:06:20.540
Um. Conditionals, Boolean algebra, etc. are perfectly cromulent mathematical objects. – Peter Taylor – 2012-04-16T07:54:01.043
@Peter Taylor: Of course they are, but the author of the other question on math.se asked for solutions that used only things you would find on a basic pocket calculator. I wanted to stay true to that restriction. – Snowball – 2012-04-16T19:29:02.490
In that case you should have disallowed exponentiation and complex numbers. Too late now, but next time you might want to post your question on the sandbox thread in meta to get a bit of feedback before people start answering.
– Peter Taylor – 2012-04-17T08:10:13.5003How 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? – None – 2016-12-19T22:07:55.710Also: It's impossible to solve this in constant time. Reading
n
takes time O(log n). VTC as unclear. – user202729 – 2018-06-07T02:07:14.243@user202729 This is incorrect. You do not actually need to read the input, since the output is periodic (with period) you only need to read the last 2 bits. Hypothetically if your input is provided as a bit stream you can do this in constant time. However it does not look like any of the answers here actually do that. – Post Rock Garf Hunter – 2018-06-07T02:39:18.847
1@HatWizard in little endian. ... not a bad point. – user202729 – 2018-06-07T02:54:59.153