Find Index of given fibonacci number in constant time

We are given a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …..
We have to find index of given Fibonacci number, i.e. like fibonacci number 8 is at index 6.

Examples :

Input : 13
Output : 7

Input : 34
Output : 9



Method 1 (Simple)
Simple approach is to find Fibonacci numbers up to given Fibonacci number and count number of iteration performed.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple C++ program to find index of given
// Fibonacci number.
#include<bits/stdc++.h>
  
int findIndex(int n)
{
    // if Fibonacci number is less than 2,
    // its index will be same as number
    if (n <= 1)
        return n;
  
    int a = 0, b = 1, c = 1;
    int res = 1;
  
    // iterate until generated fibonacci number 
    // is less than given fibonacci number
    while (c < n)
    {
        c = a + b;
          
        // res keeps track of number of generated 
        // fibonacci number
  
        res++;
        a = b;
        b = c;
    }
    return res;
}
  
// Driver program to test above function
int main()
{
    int result = findIndex(21);
    printf("%d\n", result);
}
  
// This code is contributed by Saket Kumar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple Java program to find index of 
// given Fibonacci number.
import java.io.*;
  
class GFG {
      
    static int findIndex(int n)
    {
          
        // if Fibonacci number is less 
        // than 2, its index will be
        // same as number
        if (n <= 1)
            return n;
      
        int a = 0, b = 1, c = 1;
        int res = 1;
      
        // iterate until generated fibonacci
        // number is less than given 
        // fibonacci number
        while (c < n)
        {
            c = a + b;
              
            // res keeps track of number of
            // generated fibonacci number
            res++;
            a = b;
            b = c;
        }
          
        return res;
    }
      
    // Driver program to test above function
    public static void main (String[] args) 
    {
        int result = findIndex(21);
        System.out.println( result);
    }
}
  
// This code is contributed by anuj_67.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple C# program to 
// find index of given 
// Fibonacci number.
using System;
class GFG 
{
    static int findIndex(int n)
    {
          
        // if Fibonacci number 
        // is less than 2, its 
        // index will be same 
        // as number
        if (n <= 1)
            return n;
      
        int a = 0, b = 1, c = 1;
        int res = 1;
      
        // iterate until generated 
        // fibonacci number is less 
        // than given fibonacci number
        while (c < n)
        {
            c = a + b;
              
            // res keeps track of 
            // number of generated
            // fibonacci number
            res++;
            a = b;
            b = c;
        }
          
        return res;
    }
      
    // Driver Code
    public static void Main () 
    {
        int result = findIndex(21);
        Console.WriteLine(result);
    }
}
  
// This code is contributed
// by anuj_67.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A simple Python 3 program to find 
# index of given Fibonacci number.
  
def findIndex(n) :
      
    # if Fibonacci number is less than 2,
    # its index will be same as number
    if (n <= 1) :
        return n
   
    a = 0
    b = 1
    c = 1
    res = 1
   
    # iterate until generated fibonacci number 
    # is less than given fibonacci number
    while (c < n) :
        c = a + b
           
        # res keeps track of number of  
        # generated fibonacci number
        res = res + 1
        a = b
        b = c
          
    return res
  
# Driver program to test above function
result = findIndex(21)
print(result)
  
# this code is contributed by Nikita Tiwari

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A simple PHP program to 
// find index of given
// Fibonacci number.
  
function findIndex($n)
{
      
    // if Fibonacci number
    // is less than 2,
    // its index will be 
    // same as number
    if ($n <= 1)
        return $n;
  
    $a = 0; $b = 1; $c = 1;
    $res = 1;
  
    // iterate until generated 
    // fibonacci number 
    // is less than given
    // fibonacci number
    while ($c < $n)
    {
        $c = $a + $b;
          
        // res keeps track of 
        // number of generated 
        // fibonacci number
  
        $res++;
        $a = $b;
        $b = $c;
    }
    return $res;
}
  
// Driver Code
$result = findIndex(21);
echo($result);
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

 8

 

Method 2 (Formula based)

But here, we needed to generate all the fibonacci number up to provided fibonacci number. But, there is quick solution for this problem. Let’s see how !

Note that computing log of a number is O(1) operation in most of the platforms [Source : Stackoverflow]

Fibonacci number is described as,
Fn = 1 / sqrt(5) (pow(a,n) – pow(b,n)) where
a = 1 / 2 ( 1 + sqrt(5) ) and b = 1 / 2 ( 1 – sqrt(5) )

On neglecting pow(b, n) which is very small for large value of n, we get
n = round { 2.078087 * log(Fn) + 1.672276 }
where round means round to nearest integer.

Below is the implementation of above idea.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find index of given Fibonacci
// nunber
#include<bits/stdc++.h>
  
int findIndex(int n)
{
    float fibo = 2.078087 * log(n) + 1.672276;
  
    // returning rounded off value of index
    return round(fibo);
}
  
// Driver program to test above function
int main()
{
    int n = 55;
    printf("%d\n", findIndex(n));
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple Java program to find index of given
// Fibonacci number
public class Fibonacci
{
  
  static int findIndex(int n)
  {
    float fibo = 2.078087F * (float) Math.log(n) + 1.672276F;
  
    // returning rounded off value of index
    return Math.round(fibo);
  }
  
  public static void main(String[] args)
  {
    int result = findIndex(55);
    System.out.println(result);
  }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find index of given Fibonacci
# nunber
  
import math
def findIndex(n) :
    fibo = 2.078087 * math.log(n) + 1.672276
   
    # returning rounded off value of index
    return round(fibo)
  
  
# Driver program to test above function
n = 21
print(findIndex(n))
  
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple C# program to find 
// index of given Fibonacci number
using System;
  
class Fibonacci {
  
static int findIndex(int n)
{
    float fibo = 2.078087F * (float) Math.Log(n) +
                                        1.672276F;
  
    // returning rounded off value of index
    return (int)(Math.Round(fibo));
}
  
  // Driver code
  public static void Main()
  {
    int result = findIndex(55);
    Console.Write(result);
  }
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find index
// of given Fibonacci Number
  
function findIndex($n)
{
    $fibo = 2.078087 * log($n) + 1.672276;
  
    // returning rounded off
    // value of index
    return round($fibo);
}
  
// Driver code
$n = 55;
echo(findIndex($n));
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

 10

Reference :
https://math.stackexchange.com/questions/848691/how-to-tell-if-a-fibonacci-number-has-an-even-or-odd-index

This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, jit_t, vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.