Posted on by Kalkicode
Code Mathematics

# Find index of fibonacci number

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers. The sequence starts with 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

If you are given a particular number in the Fibonacci sequence, finding its index means determining the position of that number in the sequence. For example, the number 8 is the 6th number in the Fibonacci sequence, so its index is 6.

To find the index of a given number in the Fibonacci sequence, you can use a mathematical formula that calculates the index based on the number itself. Alternatively, you can iterate through the sequence until you find the given number and count how many iterations it takes to reach it, which will give you the index.

``````//  C program for
//  Find index of fibonacci number
#include <stdio.h>
#include <math.h>

int isFibonacciNo(int n)
{
//  5n²+4
int a = 5 *n *n + 4;
//  5n²-4
int b = 5 *n *n - 4;
int aSqrt = sqrt(a);
int bSqrt = sqrt(b);
if ((aSqrt *aSqrt) == a || (bSqrt *bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return 1;
}
return 0;
}
void fibonacciIndex(int fn)
{
if (fn == 0)
{
printf("\n Given %d are exist at %d ", fn, 0);
return;
}
if (fn > 0 && isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
int n = round(log(fn) *2.078087 + 1.672276);
// Display calculated result
printf("\n Given %d are exist at %d ", fn, n);
}
else
{
printf("\n Given %d is not fibonacci number ", fn);
}
}
int main(int argc, char
const *argv[])
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
fibonacciIndex(5);
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
fibonacciIndex(233);
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
fibonacciIndex(21);
return 0;
}``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````/*
Java program
Find index of fibonacci number
*/
public class FibonacciNumber
{
public boolean isFibonacciNo(int n)
{
//  5n²+4
int a = 5 * n * n + 4;
//  5n²-4
int b = 5 * n * n - 4;
int aSqrt = (int) Math.sqrt(a);
int bSqrt = (int) Math.sqrt(b);
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
public void fibonacciIndex(int fn)
{
if (fn == 0)
{
System.out.print("\n Given " + fn + " are exist at 0");
return;
}
if (fn > 0 && isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
int n = (int)Math.round(Math.log(fn) * 2.078087 + 1.672276);
// Display calculated result
System.out.print("\n Given " + fn + " are exist at " + n);
}
else
{
System.out.print("\n Given " + fn + " is not fibonacci number ");
}
}
public static void main(String[] args)
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}
}``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````// Include header file
#include <iostream>
#include <math.h>

using namespace std;
/*
C++ program
Find index of fibonacci number
*/
class FibonacciNumber
{
public: bool isFibonacciNo(int n)
{
//  5n²+4
int a = 5 *n *n + 4;
//  5n²-4
int b = 5 *n *n - 4;
int aSqrt = (int) sqrt(a);
int bSqrt = (int) sqrt(b);
if ((aSqrt *aSqrt) == a || (bSqrt *bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
void fibonacciIndex(int fn)
{
if (fn == 0)
{
cout << "\n Given " << fn << " are exist at 0";
return;
}
if (fn > 0 && this->isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
int n = (int) round(log(fn) *2.078087 + 1.672276);
// Display calculated result
cout << "\n Given " << fn << " are exist at " << n;
}
else
{
cout << "\n Given " << fn << " is not fibonacci number ";
}
}
};
int main()
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
return 0;
}``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````// Include namespace system
using System;
/*
Csharp program
Find index of fibonacci number
*/
public class FibonacciNumber
{
public Boolean isFibonacciNo(int n)
{
//  5n²+4
int a = 5 * n * n + 4;
//  5n²-4
int b = 5 * n * n - 4;
int aSqrt = (int) Math.Sqrt(a);
int bSqrt = (int) Math.Sqrt(b);
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
public void fibonacciIndex(int fn)
{
if (fn == 0)
{
Console.Write("\n Given " + fn + " are exist at 0");
return;
}
if (fn > 0 && this.isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
int n = (int) Math.Round(Math.Log(fn) * 2.078087 + 1.672276);
// Display calculated result
Console.Write("\n Given " + fn + " are exist at " + n);
}
else
{
Console.Write("\n Given " + fn + " is not fibonacci number ");
}
}
public static void Main(String[] args)
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}
}``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````package main
import "math"
import "fmt"
/*
Go program
Find index of fibonacci number
*/
type FibonacciNumber struct {}
func getFibonacciNumber() * FibonacciNumber {
var me *FibonacciNumber = &FibonacciNumber {}
return me
}
func(this FibonacciNumber) isFibonacciNo(n int) bool {
//  5n²+4
var a int = 5 * n * n + 4
//  5n²-4
var b int = 5 * n * n - 4
var aSqrt int = int(math.Sqrt(float64(a)))
var bSqrt int = int(math.Sqrt(float64(b)))
if (aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b {
// When aSqrt or bSqrt is perfect square
return true
}
return false
}
func(this FibonacciNumber) fibonacciIndex(fn int) {
if fn == 0 {
fmt.Print("\n Given ", fn, " are exist at 0")
return
}
if fn > 0 && this.isFibonacciNo(fn) {
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
var n int = int(math.Round(
math.Log(float64(fn)) * 2.078087 + 1.672276))
// Display calculated result
fmt.Print("\n Given ", fn, " are exist at ", n)
} else {
fmt.Print("\n Given ", fn, " is not fibonacci number ")
}
}
func main() {
var task * FibonacciNumber = getFibonacciNumber()
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````<?php
/*
Php program
Find index of fibonacci number
*/
class FibonacciNumber
{
public	function isFibonacciNo(\$n)
{
//  5n²+4
\$a = 5 * \$n * \$n + 4;
//  5n²-4
\$b = 5 * \$n * \$n - 4;
\$aSqrt = (int) sqrt(\$a);
\$bSqrt = (int) sqrt(\$b);
if ((\$aSqrt * \$aSqrt) == \$a || (\$bSqrt * \$bSqrt) == \$b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
public	function fibonacciIndex(\$fn)
{
if (\$fn == 0)
{
echo("\n Given ".\$fn.
" are exist at 0");
return;
}
if (\$fn > 0 && \$this->isFibonacciNo(\$fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
\$n = (int) round(log(\$fn) * 2.078087 + 1.672276);
// Display calculated result
echo("\n Given ".\$fn.
" are exist at ".\$n);
}
else
{
echo("\n Given ".\$fn.
" is not fibonacci number ");
}
}
}

function main()
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}
main();``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````/*
Node JS program
Find index of fibonacci number
*/
class FibonacciNumber
{
isFibonacciNo(n)
{
//  5n²+4
var a = 5 * n * n + 4;
//  5n²-4
var b = 5 * n * n - 4;
var aSqrt = parseInt(Math.sqrt(a));
var bSqrt = parseInt(Math.sqrt(b));
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
fibonacciIndex(fn)
{
if (fn == 0)
{
process.stdout.write("\n Given " + fn + " are exist at 0");
return;
}
if (fn > 0 && this.isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
var n = parseInt(Math.round(Math.log(fn) * 2.078087 + 1.672276));
// Display calculated result
process.stdout.write("\n Given " + fn + " are exist at " + n);
}
else
{
process.stdout.write("\n Given " + fn + " is not fibonacci number ");
}
}
}

function main()
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}
main();``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````import math
#    Python 3 program
#    Find index of fibonacci number
class FibonacciNumber :
def isFibonacciNo(self, n) :
#   5n²+4
a = 5 * n * n + 4
#   5n²-4
b = 5 * n * n - 4
aSqrt = int(math.sqrt(a))
bSqrt = int(math.sqrt(b))
if ((aSqrt * aSqrt) == a or(bSqrt * bSqrt) == b) :
#  When aSqrt or bSqrt is perfect square
return True

return False

def fibonacciIndex(self, fn) :
if (fn == 0) :
print("\n Given ", fn ," are exist at 0", end = "")
return

if (fn > 0 and self.isFibonacciNo(fn)) :
#  When number Fibonacci and
#  Find its position
#  ???? = ⌊log(????????) *2.078087 + 1.672276⌋
n = int(round(math.log(fn) * 2.078087 + 1.672276))
#  Display calculated result
print("\n Given ", fn ," are exist at ", n, end = "")
else :
print("\n Given ", fn ," is not fibonacci number ", end = "")

def main() :
#  Test A
#  number = 5
#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
#                              ⇡
#   Index = 5
#  Test B
#  number = 5
#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
#                                                              ⇡
#   Index = 13
#  Test C
#  number = 21
#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
#                                        ⇡
#   Index = 8

if __name__ == "__main__": main()``````

#### Output

`````` Given  5  are exist at  5
Given  233  are exist at  13
Given  21  are exist at  8``````
``````#    Ruby program
#    Find index of fibonacci number
class FibonacciNumber
def isFibonacciNo(n)
#   5n²+4
a = 5 * n * n + 4
#   5n²-4
b = 5 * n * n - 4
aSqrt = Math.sqrt(a).to_i
bSqrt = Math.sqrt(b).to_i
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
#  When aSqrt or bSqrt is perfect square
return true
end

return false
end

def fibonacciIndex(fn)
if (fn == 0)
print("\n Given ", fn ," are exist at 0")
return
end

if (fn > 0 && self.isFibonacciNo(fn))
#  When number Fibonacci and
#  Find its position
#  ???? = ⌊log(????????) *2.078087 + 1.672276⌋
n = (Math.log(fn) * 2.078087 + 1.672276).round.to_i
#  Display calculated result
print("\n Given ", fn ," are exist at ", n)
else

print("\n Given ", fn ," is not fibonacci number ")
end

end

end

def main()
#  Test A
#  number = 5
#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
#                              ⇡
#   Index = 5
#  Test B
#  number = 5
#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
#                                                              ⇡
#   Index = 13
#  Test C
#  number = 21
#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
#                                        ⇡
#   Index = 8
end

main()``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````/*
Scala program
Find index of fibonacci number
*/
class FibonacciNumber()
{
def isFibonacciNo(n: Int): Boolean = {
//  5n²+4
var a: Int = 5 * n * n + 4;
//  5n²-4
var b: Int = 5 * n * n - 4;
var aSqrt: Int = scala.math.sqrt(a).toInt;
var bSqrt: Int = scala.math.sqrt(b).toInt;
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
def fibonacciIndex(fn: Int): Unit = {
if (fn == 0)
{
print("\n Given " + fn + " are exist at 0");
return;
}
if (fn > 0 && isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
var n: Int = Math.round(Math.log(fn) * 2.078087 + 1.672276).toInt;
// Display calculated result
print("\n Given " + fn + " are exist at " + n);
}
else
{
print("\n Given " + fn + " is not fibonacci number ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: FibonacciNumber = new FibonacciNumber();
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}
}``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````
``````import Foundation;
/*
Swift 4 program
Find index of fibonacci number
*/
class FibonacciNumber
{
func isFibonacciNo(_ n: Int) -> Bool
{
//  5n²+4
let a: Int = 5 * n * n + 4;
//  5n²-4
let b: Int = 5 * n * n - 4;
let aSqrt: Int = Int(Double(a).squareRoot());
let bSqrt: Int = Int(Double(b).squareRoot());
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
func fibonacciIndex(_ fn: Int)
{
if (fn == 0)
{
print("\n Given ",
fn ," are exist at 0", terminator: "");
return;
}
if (fn > 0 && self.isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
let n: Int = Int(round(log(Double(fn)) * 2.078087 + 1.672276));
// Display calculated result
print("\n Given ",
fn ," are exist at ", n, terminator: "");
}
else
{
print("\n Given ",
fn ," is not fibonacci number ", terminator: "");
}
}
}
func main()
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}
main();``````

#### Output

`````` Given  5  are exist at  5
Given  233  are exist at  13
Given  21  are exist at  8``````
``````/*
Kotlin program
Find index of fibonacci number
*/
class FibonacciNumber
{
fun isFibonacciNo(n: Int): Boolean
{
//  5n²+4
val a: Int = 5 * n * n + 4;
//  5n²-4
val b: Int = 5 * n * n - 4;
val aSqrt: Int = Math.sqrt(a.toDouble()).toInt();
val bSqrt: Int = Math.sqrt(b.toDouble()).toInt();
if ((aSqrt * aSqrt) == a || (bSqrt * bSqrt) == b)
{
// When aSqrt or bSqrt is perfect square
return true;
}
return false;
}
fun fibonacciIndex(fn: Int): Unit
{
if (fn == 0)
{
print("\n Given " + fn + " are exist at 0");
return;
}
if (fn > 0 && this.isFibonacciNo(fn))
{
// When number Fibonacci and
// Find its position
// ???? = ⌊log(????????) *2.078087 + 1.672276⌋
val n: Int = Math.round(
Math.log(fn.toDouble()) * 2.078087 + 1.672276).toInt();
// Display calculated result
print("\n Given " + fn + " are exist at " + n);
}
else
{
print("\n Given " + fn + " is not fibonacci number ");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test A
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                             ⇡
//  Index = 5
// Test B
// number = 5
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                                             ⇡
//  Index = 13
// Test C
// number = 21
// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
//                                       ⇡
//  Index = 8
}``````

#### Output

`````` Given 5 are exist at 5
Given 233 are exist at 13
Given 21 are exist at 8``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post