42
7
Introduction
We all know and love our Fibonacci sequence and have seen a myriad of challenge on it here already. However, we're still lacking a very simple case which this answer is going to provide: Reversed fibonacci! So given F_n
your job is to find n
.
Specification
Input
Your input will be a non-negative integer, which is guaranteed to be part of the fibonacci sequence.
Output
The output must be a non-negative integer as well.
What to do?
The introduction already said: Given a fibonacci number, output its index. Fiboancci number hereby is defined as F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2)
and you're given F(n)
and must return n
.
Potential Corner Cases
0 is a valid in- and output.
If given "1" as input you may either output "1" or "2", as you prefer.
You may always assume that your input actually is a fibonacci number.
You may assume that the input is representable as a 32-bit signed integer.
Who wins?
This is code-golf so the shortest answer in bytes wins!
Standard rules apply of course.
Test-cases
0 -> 0
2 -> 3
3 -> 4
5 -> 5
8 -> 6
13 -> 7
1836311903 -> 46
39
Slight nit-pick: shouldn't this be considered inverse fibonacci https://en.m.wikipedia.org/wiki/Inverse_function
– Michael – 2016-07-18T00:17:06.01319So, iccanobiF?! – None – 2016-07-18T11:24:27.933
6@Michael this is not inverse Fibonacci, because there's no inverse to Fibonacci function because it is not injective (because the "1" appears twice). The reverse originally came from the idea of "reverse table look-ups" which is what I expected people to do here (e.g. I expected them to do it to solve the problem). – SEJPM – 2016-07-18T14:19:56.290
9
The function here could be considered a right inverse of the "Fibonacci function" from the non-negative integers to the set of Fibonacci numbers. The existence of a right inverse does not imply injectivity.
– Dennis – 2016-07-18T17:31:34.197@MatthewRoh Already Taken. I already did that. :P
– mbomb007 – 2016-07-18T19:26:54.3501@SEJPM: I kinda did expect a task like "write a program that spells out the fibonacci sequence backwards", though. – Bergi – 2016-07-20T03:58:32.570
@SEJPM: It is if you limit its domain so that
1
appears only once. Sort of like howarcsin(x)
is considered to be the inverse ofsin(x)
, even thoughsin(x)
is not injective (or even surjective) in and of itself. It's all about that context! – Tim Čas – 2016-08-31T22:10:21.380