g | x | w | all
Bytes Lang Time Link
006Vyxal 3250829T132104ZThemooni
245Bespoke250829T091804ZJosiah W
003Husk250820T040530ZLegionMa
039AWK241129T185958Zxrs
066Desmos241130T235058ZYouserna
007Japt230309T175018ZShaggy
016x8664 machine code220527T032332ZChris
010UiuaSBCS240831T080811ZEurope20
004Thunno 2230723T091318ZThe Thon
054C89 clang220526T231150Zc--
004Vyxal220526T214736ZSeggan
019Regex .NET210415T210005ZDeadcode
022Factor + projecteuler.002220526T212640Zchunes
090Prolog SWI220526T212017ZDLosc
014APL Dyalog Extended201219T122421ZKamila S
005Jelly201217T221713Zcaird co
014MATL160717T200852ZLuis Men
014Brachylog190828T210428ZUnrelate
058AWK170615T175312ZRobert B
027Perl 6160717T222752ZBrad Gil
010Japt170227T183751ZOliver
038Groovy161107T152535ZMagic Oc
130C#161107T120026ZPete Ard
061Java 8161106T075706ZBananyaD
030Mathematica160831T225814ZGreg Mar
009Jelly160801T161822Zmiles
017J160718T141452Zmiles
048Perl 5.10160726T165739ZSake
084Javascript using external library160726T161110Zapplejac
049Sage160717T201134Zuser4594
070Java 7160724T063052Zdainichi
028Sesos160722T100405ZLeaky Nu
018Julia160718T070213ZDennis
089Java 7160721T142330ZKevin Cr
033JavaScript ES6160717T204619ZNeil
nan160718T112316ZKhaled.K
022JavaScript160719T131127Zcharlie
045Haskell160718T122544ZDamien
029Python160718T093255Zxnor
032Python160718T030604Zuser1648
013Pyth160718T061211ZLeaky Nu
143TSQL160718T010646ZLiesel
011Jelly160717T220431ZDJMcMayh
042JavaScript160717T200443Znicael
038Python160717T210953ZDennis
008Jelly160717T210036ZDennis
00305AB1E160717T195739ZAdnan
025Mathematica160717T195542ZLegionMa
001Actually160717T194815Zuser4594
005Pyke160717T195044ZBlue

Vyxal 3, 6 bytes

k≈⎄+}F

Vyxal It Online!

k≈⎄+}F­⁡​‎‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌­
     F  # ‎⁡index of implicit input in
  ⎄ }   # ‎⁢generator
k≈      # ‎⁣with initial vector [0,1]
   +    # ‎⁤and code +
💎

Created with the help of Luminespire.

<script type="vyxal3">
k≈⎄+}F
</script>
<script>
    args=[["0"],["2"],["3"],["5"],["8"],["13"],["1836311903"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>

Bespoke, 245 bytes

find n,F_n given
I name F_n as,from a0with a1here,an iterated total putting integer sums in it
naive solution here is:just find F_n,n-indexing by changing a counter as I go
though a formula can be possibly utilized,Bespoke cant set up log-base-b

Because Bespoke lacks floating-point numbers and a log function, iterating up to the correct Fibonacci number and counting the index along the way turns out to be the best approach here.

Husk, 3 bytes

£İf

Try it online! The İf list actually starts at 1, but £ has the helpful behavior of returning 0 when the element is not found.

AWK, 55 39 bytes

$0=int(log($1*5^.5)/log((1+5^.5)/2)+.5)

Attempt This Online!

{for(c=(i=a=0)+(b+1);a!=$1;i++){a=b;b=c;c=a+b}}1,$0=i-1
{for(c=(i=a=0)+(b+1); # 0 1 1 ...
a!=$1;i++)            # until input matched
{a=b;b=c;c=a+b}}      # move down the line
1,$0=i-1              # set output

Desmos, 66 bytes


f(x)=\prod_{n=0}^{46}\{F(n)=x:n,1\}

F(n)=\{n<2:n,F(n-1)+F(n-2)\}

Try it on Desmos!

Both expressions include the leading newline (the first includes the first two lines and the second includes the last two lines), which makes the expressions appear invisible.

Japt, 9 8 7 bytes

@¶MgX}a

Try it

x86-64 machine code, System V calling convention, 16 bytes

31 c0 99 6a 01 59 0f c1 d1 ff c0 39 fa 75 f7 c3

In assembly:

fib:
    # initialize registers: rax=0, rdx=0, rcx=1
    xor eax,eax
    cdq
    push 1
    pop rcx
fib_loop: 
    # add registers and swap over and over 
    # until the result equals the input
    xadd ecx,edx
    inc eax
    cmp edx,edi
    jne fib_loop
    ret

Try it online!

Explicitly computes the nth Fibonacci number in ecx and edx, while keeping track of the count in eax. Takes input in rdi and returns in rax, as usual for 64-bit x86.

Though it wasn't required for the challenge, this works on all 64-bit Fibonacci numbers as well. Somewhat coincidentally, as it only works since none of them are equal to each other modulo 2^32.

UiuaSBCS, 10 bytes

⌊↥0+2ₙ1.62

Try it here!

Fails at F130 = 1725375039079340637797070384.

Thunno 2, 4 bytes

ĖÆFȮ

Try it online!

Explanation

ĖÆFȮ  # Implicit input
Ė     # Push [0..input] to the stack
 ÆF   # nth Fibonacci number for each
   Ȯ  # Index of input in this list
      # Implicit output

C89 (clang), 54 bytes

a,b,r;g(f){for(a=r=0,b=1;a<f;++r)b+=a,a=b-a;return r;}

Try it online!

Vyxal, 6 4 bytes

ÞFḟ›

Try it Online!

-2 thanks to Deadcode

1 byte longer than it should be just because Vyxal's "Fibonacci sequence" builtin does not start with 0.

Regex (.NET), 23 19 bytes

(\2?(\1)|x$|^x|\b)*

Try it online!

Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the capture count of group \1.

This is based on Martin Ender's .NET regex for matching Fibonacci numbers. The .NET feature of balanced groups is used to count the number of iterations taken by the loop.

27 byte version that matches iff the input is a Fibonacci number:

^((?>\2?)(\1)\B|x$|^x|\b)*$

Try it online!

The \B prevents numbers \$1\$ less than a Fibonacci number from being matched; in unary, \B matches at every place except the beginning and end in a non-zero number (and matches "everywhere" in an input of zero).

Regex 🐇 (PCRE1 / PCRE2 v10.34 or earlier), 20 bytes

^(\2?(\1)|x$|^x)+|^x

Try it online! - PCRE1
Try it online! - PCRE2 v10.33

Takes its input in unary, as the length of a string of xs. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method. It can yield outputs bigger than the input, and is really good at multiplying.)

This is a straight port of the .NET version to the 🐇 output format. It needs to be anchored, so that the only variation in how a match can happen is how many iterations the loop takes, not where in the string it starts. The * is changed to + because with the former, looping for 0 iterations would still be a match, and would result in a 1-indexed return value.

Moving the |\b alternative to the end as |^x is a minor speed optimization, at a zero-byte cost. As an added bonus, we can change this to |^xx to return \$1\$ (instead of \$2\$) for an input of \$1\$.

For PCRE1, and PCRE2 before v10.35, the atomic group is not needed even with 🐇-output, because those versions automatically atomicize groups that contain a nested backreference (in this case, \1).

Regex 🐇 (Perl / PCRE), 23 bytes

^(\2?+(\1)|^x)+|^x|^xxx

Try it online! - Perl v5.28.2 / Attempt This Online! - Perl v5.36+
Try it online! - PCRE1
Try it online! - PCRE2 v10.33 / Attempt This Online! - PCRE2 v10.40+

Try it online! - PCRE2 - every Fibonacci number up to \$F_{46}\$ in under 20 seconds

Instead of atomicizing the whole loop, as ^((?>\2?(\1)|x$|^x|\b))+, we move all choices out of the loop, so there's only one thing it can do at any iteration. This saves 1 byte.

As in the PCRE1 version, we can change |^x to |^xx to return \$1\$ (instead of \$2\$) for an input of \$1\$.

An alternative 23 byte option would be ^(\2?+(\1)|x$|^x)+|^xxx, but that'd be ever-so-slightly slower due to having an extra choice inside the loop.

Regex (.NET), 33 29 bytes

(?=(^(x)|\3?(\1)|)*)(?<-1>x)*

Try it online!

Returns its output as the sum of the lengths of the match (group \0) and group \2. It is done as a sum because for inputs \$n=1,2,3\$ the Fibonacci index is \$n+1\$ and would not fit in a single capture group.

Group \2, which is (x) in this regex, is captured as \$1\$ iff the loop takes at least one iteration, which happens for all \$n>0\$. The loop count itself is also given an extra increment, as in the 23 byte version above, by the addition of an empty alternative | inside the loop. This will result in a zero-width match being taken at the end, regardless of input; it even happens for \$n=0\$, but that doesn't matter, because the (?<-1>x)* will not have room to pop its capture, and the return match will be \$0\$ anyway.

Note that this regex would actually not be made shorter by returning its output as the sum of the lengths of \0, \3, and \3, as the shortest way I can think of to do that takes 34 bytes:

(?=((?>\2?)(\1)|^x)*(x))?(?<-1>x)*

Try it online!

38 byte version that matches iff the input is a Fibonacci number:

^(?=(^(x)|(?>\3?)(\1)|)*x$|$)(?<-1>x)*

Try it online!

Regex (Perl / PCRE), 31 bytes

((?=(\2?x))(^x|\4?+(\3)))*(x|^)

Try it online! - Perl
Try it online! - PCRE

Returns its output as the sum of the lengths of groups \2, \5, and \5. The regex saves 3 bytes just by using a possessive quantifier, replacing (?>\4?) with \4?+.

The Fibonacci index minus \$2\$ is built up in \2 using the expression (?=(\2?x)). The first time this is hit, the \2? will evaluate to zero because \2 isn't set yet, and \2 will be given a value of \$1\$. On every subsequent iteration, it will be incremented. This is done in a lookahead to avoid influencing the running total Fibonacci match, and is safe because at every step, the index will be less than the amount added to the full match.

The (x|^) at the end serves to let \5\$=1\$ in all cases except \$n=0\$.

This regex does not work under Java; group \2 just captures \$1\$ and stays there. This may be due to a bug in Java's regex engine.

33 byte version that matches iff the input is a Fibonacci number:

^((?=(\2?x))(^x|\4?+(\3)))*(x|^)$

Try it online! - Perl
Try it online! - PCRE

If the output specification must be simplified to sum only two groups, this would be the shortest way I can think of doing it (39 bytes, output is the sum of the lengths of groups \0 and \5):

(?=((?=(\2?x))(^x|\4?+(\3)))*(x))?\2?x?

Try it online! - Perl
Try it online! - PCRE

42 byte version that matches iff the input is a Fibonacci number:

^(?=((?=(\2?x))(^x|\4?+(\3)))*(x|^)$)\2?x?

Try it online! - Perl
Try it online! - PCRE

Factor + project-euler.002, 23 22 bytes

[ dup fib-upto index ]

Try it online!

Prolog (SWI), 90 bytes

Written in conversation with flawr in chat.

F-N-A-B:-F=A;N>0,M is N-1,C is A+B,F-M-B-C.
F/N/I:-N=I,F-N-0-1;J is I+1,F/N/J.
F/N:-F/N/0.

Try it online!

Ungolfed

Here's a less-golfed version:

fib(F,0,F,_).
fib(F,N,A,B) :- N>0, M is N-1, C is A+B, fib(F,M,B,C).
fibindex(F,N,N) :- fib(F,N,0,1).
fibindex(F,N,I) :- J is I+1, fibindex(F,N,J).
fibindex(F,N) :- fibindex(F,N,0).

The fib/4 predicate (F-N-A-B in the golfed version) takes a number N and two accumulators A and B (which should start at 0 and 1) and unifies F with the Nth Fibonacci number. If N is 0, F is simply the current value of A, the smaller accumulator value. Otherwise, count N down by one and recurse with new accumulator values B and A+B.

fibindex/3 (F/N/I in the golfed version) takes a Fibonacci number F and an index I (which should start at 0) and unifies N with the Fibonacci index of F. Either F is the Ith Fibonacci number, in which case N=I and we're done, or we count I up by one and recurse. Note that this approach leads to infinite recursion if given a non-Fibonacci number, but we don't have to handle that case for this challenge.

Finally, fibindex/2 (F/N) just calls fibindex/3 with an initial I value of 0.

APL (Dyalog Extended), 14 bytes

(+.!∘⌽⍨∘⍳¨…)⍳⊢

Explanation soon (TM)

Try it online!

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

MATL, 14 bytes

t?5X^*17L&YlYo

Try it online!

This uses an inverse of Binet's formula, and so it's very fast.

Let F denote the n-th Fibonacci number, and φ the golden ratio. Then

enter image description here

The code uses this formula with two modifications:

How it's done

t         % Take input F implicitly. Make a copy
?         % If (copy of) F is positive
  5X^     %   Push sqrt(5)
  *       %   Multiply by F
  17L     %   Push phi (predefined literal)
  &Yl     %   Two-input logarithm: first input is argument, second is base
  Yo      %   Round towards nearest integer
          % Else the input, which is 0, is left on the stack
          % End if implicitly
          % Display implicitly

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.

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!

Perl 6  33 30  27 bytes

{first *==$_,:k,(0,1,*+*...*>$_)}
{first *==$_,:k,(0,1,*+*...*)}
{first $_,:k,(0,1,*+*...*)}

Try it

Explanation:

# lambda with implicit 「$_」 parameter
{
  first           # find the first element
    $_,           # where something is equal to the block's argument
    :k,           # return the key rather than the value

    # of the Fibonacci sequence
    ( 0, 1, * + * ... * )
    # ^--^ first two values
    #       ^---^ lambda used to generate the next in the series
    #             ^-^ generate until
    #                 ^ Whatever
}

Test:

#! /usr/bin/env perl6
use v6.c;
use Test;

# using the safer version that stops generating
# values bigger than the input
my &fib-index = {first $_,:k,(0,1,*+*...*>$_)}

my @tests = (
  0 => 0,
  2 => 3,
  3 => 4,
  5 => 5,
  8 => 6,
  13 => 7,
  1836311903 => 46,
  1836311904 => Nil, # this is why the safe version is used here
  12200160415121876738 => 93,
  19740274219868223167 => 94,
  354224848179261915075 => 100,
);

plan +@tests + 1;

for @tests -> $_ ( :key($input), :value($expected) ) {
  cmp-ok fib-index($input), &[eqv], $expected, .gist
}

cmp-ok fib-index((0,1,*+*...*)[1000]), &[eqv], 1000, 'works up to 1000th element of Fibonacci sequence'
1..13
ok 1 - 0 => 0
ok 2 - 2 => 3
ok 3 - 3 => 4
ok 4 - 5 => 5
ok 5 - 8 => 6
ok 6 - 13 => 7
ok 7 - 1836311903 => 46
ok 8 - 1836311904 => Nil
ok 9 - 12200160415121876738 => 93
ok 10 - 19740274219868223167 => 94
ok 11 - 354224848179261915075 => 100
ok 12 - works up to 1000th element of Fibonacci sequence

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

Groovy, 38 bytes

{a=c=0;b=1;for(;a<it;b+=(a=b-a))c++;c}

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

Java 8 61 bytes

Same as @dainichi answer only made shorter by using Java 8 lambdas. The answer is a valid rvalue expression.

n->{int a=0,b=1,c=0,t;while(a<n){c++;t=b;b+=a;a=t;}return c;}

Ungolfed:

interface F
{
    int c(int n);
}

public class Main
{

    public static void main(String[] args)
    {
        F f = n->{int a=0,b=1,c=0,t;while(a<n){c++;t=b;b+=a;a=t;}return c;};
    }
}

Mathematica, 30 bytes

Round@Log[5^.5/2+.5,.8+5^.5#]&

Pure function; returns 2 if the input is 1.

Doesn't beat the other Mathematica entry, but showcases an unusual method: It's a (very cool) fact that the Nth Fibonacci number is the closest integer to [1/sqrt(5) times the Nth power of the golden ratio] ("Binet's formula").

Therefore the inverse function will be the base-[golden ratio] logarithm of [sqrt(5) times the Fibonacci number in question]. The .8+ is a hack to make sure we don't take the logarithm of 0, without screwing up the other values.

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

J, 32 27 17 bytes

i.~0,+/@(!|.)\@i.

Computes the first n Fibonacci numbers and then finds the index of n in that list.

Usage

Extra commands are used for formatting multiple input/output. The last test case is omitted since it will require much more time to compute.

   f =: i.~0,+/@(!|.)\@i.
   (,.f"0) 0 1 2 3 5 8 13
 0 0
 1 1
 2 3
 3 4
 5 5
 8 6
13 7

Explanation

i.~0,+/@(!|.)\@i.  Input: n
               i.  Get the range [0, 1, ..., n-1]
             \@    For each prefix of that range
          |.         Reverse the prefix
         !           Find the binomial coefficient between each value in the original
                     prefix and the reversed prefix
     +/@             Sum those binomial coefficients
                   This will create the Fibonacci numbers from 1 to n
   0,              Prepend a 0 to the list of Fibonacci numbers
i.~                Find the index of n in that list and return

Perl 5.10, 48 bytes

Basically looking for the right n so that F(n) = input.

-a switch adds one byte.

$b++;while($_>$a){$c=$a;$a+=$b;$b=$c;$n++}say$n

Try it here!

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

Link to lib: https://github.com/mvegh1/Enumerable

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

enter image description here

Sage, 49 bytes

lambda x,s=sqrt(5):x and int(log(x*s,(1+s)/2)+.5)

Thanks to TuukkaX for the suggestion about saving sqrt(5) as s to shave off a few bytes.

Try it online.

This approach using an inverse of Binet's formula offers several improvements over the previous approach: it's faster (constant-time versus quadratic time), it actually works for larger inputs, and it's shorter!

Python users may wonder why I'm using sqrt(5) instead of the shorter 5**.5 - it's because 5**.5 is computed with C's pow function, and loses precision due to floating point issues. Many mathematical functions (including sqrt and log) are overloaded in Sage to return an exact, symbolic value, which don't lose precision.

Java 7, 70 bytes

int c(int n){int a=0,b=1,c=0,t;while(a<n){c++;t=b;b+=a;a=t;}return c;}

https://ideone.com/I4rUC5

Sesos, 28 bytes

Hexdump:

0000000: 16f8be 766ef7 ae6d80 f90bde b563f0 7ded18 3ceffa  ...vn..m.....c.}..<..
0000015: b1c1bb af9f3f ff                                  .....?.

Try it online!

(Exponential time because in Sesos copying a number needs exponential time.)

Assembly used to generate the binary file:

set numin
set numout
get
jmp
sub 1
fwd 1
add 1
fwd 1
add 1
rwd 2
jnz    ;input input
fwd 4
add 1  ;input input 0 1
fwd 2
add 1  ;input input 0 1 0 1
rwd 4
jmp
jmp    ;input input-curr curr next iterations
sub 1
jnz    ;input 0 curr next iterations
fwd 3
add 1
jmp
sub 1
fwd 2
add 1
rwd 2
jnz    ;input 0 curr next 0 0 iterations+1
rwd 1
jmp
sub 1
fwd 1
add 1
fwd 1
add 1
rwd 2
jnz    ;input 0 curr 0 next next iterations+1
rwd 1
jmp
sub 1
fwd 1
sub 1
fwd 2
add 1
rwd 3
jnz    ;input 0 0 -curr next curr+next iterations+1
rwd 2
jmp
sub 1
fwd 2
add 1
fwd 1
add 1
rwd 3
jnz    ;0 0 input input-curr next curr+next iterations+1
fwd 3
jnz
fwd 3
put

Julia, 27 26 18 bytes

!n=log(3n+.7)÷.48

This uses the inverse of Binet's formula, with just enough precision for 32-bit integers; it actually works up to F(153) = 42,230,279,526,998,466,217,810,220,532,898 > 2105.

Try it online!

How it works

Binet's formula states the following.

Binet's formula

Restricting F to the set of Fibonacci, the map n → Fn has a right inverse F → nF.

We have that

right inverse of Binet's formula

and all that is left to do is dealing with the edge case 0.

Since the input is restricted to 32-bit integers, we can use short decimal literals instead of the constants in the formula.

Java 7, 89 bytes

int c(int n){int i=-1;while(f(++i)<n);return i;}int f(int n){return n<2?n:f(n-1)+f(n-2);}

Inspired by the explanation of @Adnan's 05AB1E answer.

Ungolfed & test cases:

Try it here. (Time limit exceeded for last test case, but it works in about 30-45 seconds on my PC.)

class Main{
  static int c(int n){
    int i = -1;
    while(f(++i) < n);
    return i;
  }

  static int f(int n){
    return n < 2
             ? n
             : f(n - 1) + f(n - 2);
  }

  public static void main(String[] a){
    System.out.println(c(0));
    System.out.println(c(2));
    System.out.println(c(3));
    System.out.println(c(5));
    System.out.println(c(8));
    System.out.println(c(1836311903));
  }
}

Output:

0
3
4
5
6
46

JavaScript (ES6), 39 33 bytes

f=(n,j=0,k=1)=>n>j?f(n,k,j+k)+1:0

Even with ES7, the inverse Binet formula takes 47 bytes:

x=>Math.log(x*5**.5)/Math.log(.5+1.25**.5)+.5|0
x=>Math.log(x*5**.5)/Math.log((1+5**.5)/2)+.5|0
x=>Math.log(x*(p=5**.5))/Math.log((1+p)/2)+.5|0

C, 62 58 bytes

g(c,a,b){return c-a?g(c,b,a+b)+1:0;}f(c){return g(c,0,1);}

Detailed

int g(int c, int a, int b)
{
    if (c == a)
    {
        return 0;
    }
    else
    {
        return g(c, b, a+b) + 1;
    }
}

int f(c)
{
    return g(c, 0, 1);
}

JavaScript, 22 bytes

n=>Math.log(n)/.48+2|0

Haskell, 45 bytes

f x=round$log(sqrt 5*x+0.9)/log((sqrt 5+1)/2)

Python, 29 bytes

g=lambda n:n>.7and-~g(n/1.61)

Recursively divides the input by the golden-ratio approximation 1.61 until it's below 0.7, and outputs the number of divisions.

For 0, the code outputs False, which equals 0 in Python. This can be avoided for 2 bytes

g=lambda n:n//.7and 1+g(n/1.61)

Python, 36 34 32 bytes

lambda n:len(str(66*n**6))//1.24

Previous versions:

f=lambda n:len(str(66*n**6))//1.24
f=lambda n:(n*n*7).bit_length()//1.4

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

Then the divisor was truncated to as little precision as I could manage and the multiplier chosen to give the correct results for all 32-bit fibonacci numbers.

Pyth, 13 bytes

J1tf>=Z+~JZZQ

Test suite.

Approximation in Python 2:

Z=0;J=1;T=1;Q=input()
while not J+Z>Q:
    temp=J
    J=Z
    Z=temp+J
    T += 1
print(T-1)

alternative approach, 18 bytes

L?<b2bsyMtBtbs.IyG

Test suite.

This uses .I for inverse.

TSQL, 143 bytes

Input goes in @n as in DECLARE @n INT = 1836311903;

DECLARE @O BIGINT=0;WITH F(R,P,N)AS(SELECT @O,@O,@O+1 UNION ALL SELECT R+1,N,P+N FROM F WHERE N<=@n)SELECT MAX(R)FROM F OPTION(MAXRECURSION 0);

Jelly, 14 11 bytes

5½×lØp+.Ḟ»0

Try it online!

This is my first ever Jelly answer! This uses the algorithm from the MATL answer. Thanks to Dennis for shaving off 3 bytes!

Explanation:

   lØp      # Log Base phi
5½          # Of the square root of 5
  ×         # Times the input
      +     # Plus
       .    # 0.5
        Ḟ   # Floored

This gets the right answer, now we just need to handle the special case of '0'. With '0' as an arg, we get -infinity, so we return

»      # The maximum of 
 0     # Zero
       # And the previous calculated value.

JavaScript, 54 50 69 50 42 bytes

b=>(j=>{for(i=c=0;b-i;c++)i=j+(j=i)})(1)|c

Surely it isn't going to win, just for fun :)

Ok, checking for zero consumes 19 bytes. WTF? Stupid-me.


Demo! To see the last test case, you have to scroll the console a bit.

a=b=>(j=>{for(i=c=0;b-i;c++)i=j+(j=i)})(1)|c;
console.log('0: '+a(0));
console.log('2: '+a(2));
console.log('3: '+a(3));
console.log('5: '+a(5));
console.log('8: '+a(8));
console.log('13: '+a(13));
console.log('1836311903: '+a(1836311903));

Thanks @edc for shortening by 8 bytes.

Python, 38 bytes

f=lambda n,a=0,b=1:n^a and-~f(n,b,a+b)

Test it on Ideone.

Jelly, 8 bytes

1+С0
¢i

Try it online! Note that this approach is too inefficient for the last test case.

How it works

¢i     Main link. Argument: n

¢      Call the helper link niladically (i.e., without arguments).
       This yields the sequence of the first n positive Fibonacci numbers, i.e.,
       [1, 1, 2, 3, 5, ...].
 i     Find the first index of n (1-based, 0 if not found).


1+С0  Helper link. No arguments.

1      Set the left argument to 1.
    0  Yield 0.
 +С   Add both arguments, replacing the left argument with the sum and the right
       argument with the previous value of the left argument.
       Yield the array of all intermediate values of the left argument.

05AB1E, 3 bytes

Code:

ÅFg

Explanation:

ÅF   # Generate all Fibonacci numbers <= input.
  g  # Get the length of this list.

Uses the CP-1252 encoding. Try it online!.

Mathematica, 25 bytes

InverseFunction@Fibonacci

Function. Pretty self-explanatory if you ask me.

Actually, 1 byte

f

Yes, there's a builtin for this, since November 16, 2015.

Try it online


For fun, without the builtin, it's 9 bytes:

╗1`F╜=`╓i

Try it online!

Explanation:

╗1`F╜=`╓i
╗          push input to register 0
 1`F╜=`╓   push list containing first value x (starting with x = 0) where:
   F         fib(x)
    ╜=       is equal to the input
        i  flatten the list

Pyke, 5 bytes

.f.bq

Try it here!

.f    - first number where
  .b  -  fib(n)
    q - ^ == input