28
1
You probably all know the fibonacci sequence:
fibonacci(n)=fibonacci(n-1)+fibonacci(n-2)
fibonacci(0)=0
fibonacci(1)=1
Your task is as simple as it could be:
- Given integer
N
computefibonacci(n)
but here is the twist:
- Also do negative
N
Wait. What?
fibonacci(1)=fibonacci(0)+fibonacci(-1)
so
fibonacci(-1)=1
and
fibonacci(-2)=fibonacci(0)-fibonacci(1)=-1
and so on...
- This is a code-golf so shortest programm in bytes win.
- You may submit a function or a full programm
- N is in [-100,100]
Testcase(s) in CSV:
-9;-8;-7;-6;-5;-4;-3;-2;-1;0;1;2;3;4;5;6;7;8
34;-21;13;-8;5;-3;2;-1;1;0;1;1;2;3;5;8;13;21
Hint:
n<0 and n&1==0:
fibonacci(n)=fibonacci(abs(n))*-1
No. Mine wants you to support negative numbers too. – Roman Gräf – 2016-12-31T10:52:17.587
7I think this is not a dupe. Of the answers on the first page of the existing Fibonacci challenge, only 1 can handle negatives, and all the rest would need to be significantly changed to go backwards too. – xnor – 2016-12-31T11:01:12.930
Added some. Feel free to add more. @Flip – Roman Gräf – 2016-12-31T12:02:31.443
1
Read this meta post about formatting test cases: try to avoid fancy tables
– FlipTack – 2016-12-31T12:08:29.570and by CSV you mean SSV (semicolon separated values)? – NH. – 2016-12-31T13:55:40.350
@NH. "European CSV" https://en.wikipedia.org/wiki/Comma-separated_values#Example
– Slai – 2017-01-01T05:54:58.650