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.
true
for1
? \$\endgroup\$1
values count as initial values for the output? Or do we start directly with the2
? \$\endgroup\$