42
\$\begingroup\$

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

35 Answers 35

1
2
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Groovy, 38 bytes

{a=c=0;b=1;for(;a<it;b+=(a=b-a))c++;c}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl, 33 +1 = 34 bytes

Run with the -p flag

$_=int log(.5+$_*5**.5)/log 1.618

The code uses an approximation of the golden ratio as 1.618, and calculates the index using the following formula:

Fibonacci index formula

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.