# Fibonacci reversed!

## 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

• Slight nit-pick: shouldn't this be considered inverse fibonacci en.m.wikipedia.org/wiki/Inverse_function Jul 18 '16 at 0:17
• So, iccanobiF?!
– user54200
Jul 18 '16 at 11:24
• @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). Jul 18 '16 at 14:19
• 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. Jul 18 '16 at 17:31
• @SEJPM: I kinda did expect a task like "write a program that spells out the fibonacci sequence backwards", though. Jul 20 '16 at 3:58

# Japt, 10 bytes

Lo æ@U¥MgX


Try it online!

## Explanation

Lo æ@U¥MgX
Lo           // Creates a range from 0 to 99
æ@        // Iterates through the range. Returns the first item X where:
U¥      //   Input ==
MgX   //   Xth Fibonacci number


# Brachylog, 14 bytes

≜∧0;1⟨t≡+⟩ⁱ↖?h


Try it online!

Takes input through the output variable and outputs through the input variable.

≜                 Label the input variable, trying 0, 1, -1, 2...,
0               then starting with 0
∧                (which is not necessarily the input variable)
;1             paired with 1,
⟨t≡ ⟩        replace the first element of the pair with the last element
⟨ ≡+⟩        and the last element of the pair with the sum of the elements
ⁱ↖?     a number of times equal to the input variable,
h    such that the first element of the pair is the output variable.


I'm not entirely sure why ≜ is necessary.

# Jelly, 5 bytes

‘ÆḞ€i


Try it online!

Nothing special here, just an updated version of Jelly

## How it works

‘ÆḞ€i - Main link. Takes n on the left
‘     - Yield n+1
€  - For each k = 1, 2, ..., n+1:
ÆḞ   -   Yield the k'th Fibonacci number
i - Return the index of n in this list


## Javascript (using external library) (84 bytes)

n=>_.Until((i,a)=>{l=a.length;if(a[l-1]!=n){return i<=1?i:a[l-1]+a[l-2]}}).Count()-1


Code explanation: Library has static method that creates a sequence until the predicate has an undefined return value. The predicate has a signature of ("i"ndex, current internal "a"rray generated). At each iteration, we check if the last element of the internal array is equal to the input, n. If not, return the next value in the fib sequence. Otherwise, the predicate has an undefined result which terminates the generation of the sequence. Then, we return the length of the sequence (and subtract 1 in order to comply with the 0 based-ness as seen in the OP

• 53 Bytes by using code from here n=>{a=c=t=0,b=1;while(a<n){c++;t=b;b+=a;a=t}return c} Try it online! Aug 29 '19 at 13:17

# Jelly, 9 bytes

‘RḶUc$S€i  Finds the first n+1 Fibonacci numbers and locates the index of n in that list. Note: This is very inefficient and large test cases should not be run on the online interpreter. Try it here. ## Explanation ‘RḶUc$S€i  Input: n
‘          Increment n
R         Generate the range [1, 2, ..., n+1]
For each value x in that range
Ḷ          Create the range [0, 1, ..., x-1]
U         Create a reversed copy
c        Compute the binomial coefficient between each pair of values
$Combine the last two links (Uc) as a monad S€ Sum each list of binomial coefficients This will result in a list of the first n+1 Fibonacci numbers i Find the index of n in that list and return  # C#, 130 Bytes Golfed: int F(int n){var a=new int[4];a[1]=1;int i=0;while(a[3]<n){a[3]=a.ToList().GetRange(1,2).Sum();a[1]=a[2];a[2]=a[3];i++;}return i;}  Ungolfed: public int F(int n) { var a = new int[4]; a[1] = 1; int i = 0; while (a[3] < n) { a[3] = a.ToList().GetRange(1, 2).Sum(); a[1] = a[2]; a[2] = a[3]; i++; } return i; }  Test: var fibonacciReversed = new FibonacciReversed(); var fr = fibonacciReversed.F(0); Console.WriteLine(fr); 0 fr = fibonacciReversed.F(2); Console.WriteLine(fr); 3 fr = fibonacciReversed.F(3); Console.WriteLine(fr); 4 fr = fibonacciReversed.F(5); Console.WriteLine(fr); 5 fr = fibonacciReversed.F(8); Console.WriteLine(fr); 6 fr = fibonacciReversed.F(13); Console.WriteLine(fr); 7 fr = fibonacciReversed.F(1836311903); Console.WriteLine(fr); 46  # AWK, 58 bytes {for(n[++j]++;n[j]<$1;n[++j]=n[j]+n[j-1]){}j=$0?j:0;$0=j}1


Try it online!