17
1
In the alternating Fibonacci sequence, you first start with 1
and 1
as usual.
However, instead of always adding the last two values to get the next number, you alternate starting with adding, and every other time you subtract instead.
The sequence starts like this:
1
1
2 # 1 + 1
-1 # 1 - 2
1 # 2 + -1
-2 # -1 - 1
-1 # 1 + -2
-1 # -2 - -1
-2 # -1 + -1
1 # -1 - -2
-1 # -2 + 1
2 # 1 - -1
1 # -1 + 2
1 # 2 - 1
etc.
Notice that after it starts over once it gets to 1
and 1
again.
Given a number N, print the Nth term of the alternating fibonacci sequence.
Remember, this is code-golf, so the code with the smallest number of bytes wins.
Is the sequence 0-indexed or 1-indexed (or either one)? – Doorknob – 2016-11-08T23:47:38.400
@Doorknob Either one. Specify in your answer. – Oliver Ni – 2016-11-08T23:48:22.497
Can we return
true
for1
? – ETHproductions – 2016-11-08T23:57:00.600Do the first two
1
values count as initial values for the output? Or do we start directly with the2
? – Luis Mendo – 2016-11-08T23:58:53.777@LuisMendo The first two count. – Oliver Ni – 2016-11-09T00:01:56.600