Find Index of given fibonacci number in constant time
Last Updated :
26 Oct, 2023
We are given a Fibonacci number. The first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …..
We have to find the index of the 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) : A simple approach is to find Fibonacci numbers up to the given Fibonacci numbers and count the number of iterations performed.
C++
#include<bits/stdc++.h>
int findIndex( int n)
{
if (n <= 1)
return n;
int a = 0, b = 1, c = 1;
int res = 1;
while (c < n)
{
c = a + b;
res++;
a = b;
b = c;
}
return res;
}
int main()
{
int result = findIndex(21);
printf ( "%d\n" , result);
}
|
Java
import java.io.*;
class GFG {
static int findIndex( int n)
{
if (n <= 1 )
return n;
int a = 0 , b = 1 , c = 1 ;
int res = 1 ;
while (c < n)
{
c = a + b;
res++;
a = b;
b = c;
}
return res;
}
public static void main (String[] args)
{
int result = findIndex( 21 );
System.out.println( result);
}
}
|
Python3
def findIndex(n) :
if (n < = 1 ) :
return n
a = 0
b = 1
c = 1
res = 1
while (c < n) :
c = a + b
res = res + 1
a = b
b = c
return res
result = findIndex( 21 )
print (result)
|
C#
using System;
class GFG
{
static int findIndex( int n)
{
if (n <= 1)
return n;
int a = 0, b = 1, c = 1;
int res = 1;
while (c < n)
{
c = a + b;
res++;
a = b;
b = c;
}
return res;
}
public static void Main ()
{
int result = findIndex(21);
Console.WriteLine(result);
}
}
|
Javascript
<script>
function findIndex(n)
{
if (n <= 1)
return n;
let a = 0, b = 1, c = 1;
let res = 1;
while (c < n)
{
c = a + b;
res++;
a = b;
b = c;
}
return res;
}
let result = findIndex(21);
document.write(result);
</script>
|
PHP
<?php
function findIndex( $n )
{
if ( $n <= 1)
return $n ;
$a = 0; $b = 1; $c = 1;
$res = 1;
while ( $c < $n )
{
$c = $a + $b ;
$res ++;
$a = $b ;
$b = $c ;
}
return $res ;
}
$result = findIndex(21);
echo ( $result );
?>
|
Method 2 (Formula based)
But here, we needed to generate all the Fibonacci numbers up to a provided Fibonacci number. But, there is a quick solution to this problem. Let’s see how! Note that computing the log of a number is an O(1) operation on most platforms.
The 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 because of the large value of n, we get
n = round { 2.078087 * log(Fn) + 1.672276 }
where round means round to the nearest integer.
Below is the implementation of the above idea.
C++
#include<bits/stdc++.h>
int findIndex( int n)
{
float fibo = 2.078087 * log (n) + 1.672276;
return round(fibo);
}
int main()
{
int n = 55;
printf ( "%d\n" , findIndex(n));
}
|
Java
public class Fibonacci
{
static int findIndex( int n)
{
float fibo = 2 .078087F * ( float ) Math.log(n) + 1 .672276F;
return Math.round(fibo);
}
public static void main(String[] args)
{
int result = findIndex( 55 );
System.out.println(result);
}
}
|
Python3
import math
def findIndex(n) :
fibo = 2.078087 * math.log(n) + 1.672276
return round (fibo)
n = 21
print (findIndex(n))
|
C#
using System;
class Fibonacci {
static int findIndex( int n)
{
float fibo = 2.078087F * ( float ) Math.Log(n) +
1.672276F;
return ( int )(Math.Round(fibo));
}
public static void Main()
{
int result = findIndex(55);
Console.Write(result);
}
}
|
Javascript
<script>
function findIndex(n)
{
var fibo = 2.078087 * parseFloat(Math.log(n)) + 1.672276;
return Math.round(fibo);
}
var result = findIndex(55);
document.write(result);
</script>
|
PHP
<?php
function findIndex( $n )
{
$fibo = 2.078087 * log( $n ) + 1.672276;
return round ( $fibo );
}
$n = 55;
echo (findIndex( $n ));
?>
|
Time complexity: O(1)
Auxiliary space: O(1)
Approach:
we can solve this problem using the formula for the nth Fibonacci number, which is:
F(n) = (pow((1+sqrt(5))/2, n) – pow((1-sqrt(5))/2, n)) / sqrt(5)
We can derive the index of a given Fibonacci number using this formula. We can iterate over the values of n and calculate the corresponding Fibonacci number using the above formula until we find a Fibonacci number that is greater than or equal to the given number. At this point, we can return the index of the Fibonacci number that matches the given number.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <cmath>
using namespace std;
int findIndex( int n) {
double phi = (1 + sqrt (5)) / 2;
int index = round( log (n * sqrt (5) + 0.5) / log (phi));
int fib = round(( pow (phi, index) - pow (1 - phi, index)) / sqrt (5));
if (fib == n)
return index;
else
return -1;
}
int main() {
int n = 34;
int index = findIndex(n);
cout << "The index of " << n << " is " << index << endl;
return 0;
}
|
Java
import java.util.*;
public class FibonacciIndex {
public static int findIndex( int n) {
double phi = ( 1 + Math.sqrt( 5 )) / 2 ;
int index = ( int ) Math.round(Math.log(n * Math.sqrt( 5 ) + 0.5 ) / Math.log(phi));
int fib = ( int ) Math.round((Math.pow(phi, index) - Math.pow( 1 - phi, index)) / Math.sqrt( 5 ));
if (fib == n)
return index;
else
return - 1 ;
}
public static void main(String[] args) {
int n = 34 ;
int index = findIndex(n);
System.out.println( "The index of " + n + " is " + index);
}
}
|
Python3
import math
def find_index(n):
phi = ( 1 + math.sqrt( 5 )) / 2
index = round (math.log(n * math.sqrt( 5 ) + 0.5 ) / math.log(phi))
fib = round ((math. pow (phi, index) - math. pow ( 1 - phi, index)) / math.sqrt( 5 ))
if fib = = n:
return index
else :
return - 1
def main():
n = 34
index = find_index(n)
print (f "The index of {n} is {index}" )
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static int FindIndex( int n)
{
double phi = (1 + Math.Sqrt(5)) / 2;
int index = ( int )Math.Round(Math.Log(n * Math.Sqrt(5) + 0.5) / Math.Log(phi));
int fib = ( int )Math.Round((Math.Pow(phi, index) - Math.Pow(1 - phi, index)) / Math.Sqrt(5));
if (fib == n)
return index;
else
return -1;
}
static void Main()
{
int n = 34;
int index = FindIndex(n);
Console.WriteLine( "The index of " + n + " is " + index);
}
}
|
Javascript
function findIndex(n) {
const phi = (1 + Math.sqrt(5)) / 2;
const index = Math.round(Math.log(n * Math.sqrt(5) + 0.5) / Math.log(phi));
const fib = Math.round((Math.pow(phi, index) - Math.pow(1 - phi, index)) / Math.sqrt(5));
if (fib === n) {
return index;
} else {
return -1;
}
}
function main() {
const n = 34;
const index = findIndex(n);
console.log( "The index of " + n + " is " + index);
}
main();
|
Output
The index of 34 is 9
Time Complexity: O(1) as it involves only a few arithmetic operations.
Space Complexity: O(1) as it uses only a constant amount of memory to store the variables.
This article is contributed by Aditya Kumar.
Share your thoughts in the comments
Please Login to comment...