16
Introduction
The sign of a number is either a +
, or a -
for every non-zero integer. Zero itself is signless (+0
is the same as -0
). In the following sequence, we are going to alternate between the positive sign, the zero and the negative sign. The sequence starts with 1
, so we write 1
with a positive sign, with zero (this one is weird, but we just multiply the number by 0) and the negative sign:
1, 0, -1
The next number is 2
, and we do the same thing again:
2, 0, -2
The sequence eventually is:
1, 0, -1, 2, 0, -2, 3, 0, -3, 4, 0, -4, 5, 0, -5, 6, 0, -6, 7, 0, -7, ...
Or a more readable form:
a(0) = 1
a(1) = 0
a(2) = -1
a(3) = 2
a(4) = 0
a(5) = -2
a(6) = 3
a(7) = 0
a(8) = -3
a(9) = 4
...
The Task
Given a non-negative integer n, output the nth term of the above sequence. You can choose if you use the zero-indexed or one-indexed version.
Test cases:
Zero-indexed:
a(0) = 1
a(11) = -4
a(76) = 0
a(134) = -45
a(296) = -99
Or if you prefer one-indexed:
a(1) = 1
a(12) = -4
a(77) = 0
a(135) = -45
a(297) = -99
This is code-golf, so the submission with the smallest number of bytes wins!
Is it Ok if you start with
[0, 0, 0, -1, 0, 1...
– Blue – 2016-05-28T16:54:30.977@muddyfish no sorry, it has to start with
1
. – Adnan – 2016-05-28T16:59:17.437