Find index of fibonacci number

Here given code implementation process.

//  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)
	{
		FibonacciNumber task = new FibonacciNumber();
		// Test A
		// number = 5    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                             ⇡
		//  Index = 5              
		task.fibonacciIndex(5);
		// Test B
		// number = 5    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                                                             ⇡
		//  Index = 13  
		task.fibonacciIndex(233);
		// Test C
		// number = 21    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                                       ⇡
		//  Index = 8  
		task.fibonacciIndex(21);
	}
}

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()
{
	FibonacciNumber *task = new FibonacciNumber();
	// Test A
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                             ⇡
	//  Index = 5              
	task->fibonacciIndex(5);
	// Test B
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                                             ⇡
	//  Index = 13  
	task->fibonacciIndex(233);
	// Test C
	// number = 21    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                       ⇡
	//  Index = 8  
	task->fibonacciIndex(21);
	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)
	{
		FibonacciNumber task = new FibonacciNumber();
		// Test A
		// number = 5    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                             ⇡
		//  Index = 5              
		task.fibonacciIndex(5);
		// Test B
		// number = 5    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                                                             ⇡
		//  Index = 13  
		task.fibonacciIndex(233);
		// Test C
		// number = 21    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                                       ⇡
		//  Index = 8  
		task.fibonacciIndex(21);
	}
}

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              
	task.fibonacciIndex(5)
	// Test B
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                                             ⇡
	//  Index = 13  
	task.fibonacciIndex(233)
	// Test C
	// number = 21    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                       ⇡
	//  Index = 8  
	task.fibonacciIndex(21)
}

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()
{
	$task = new FibonacciNumber();
	// Test A
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                             ⇡
	//  Index = 5              
	$task->fibonacciIndex(5);
	// Test B
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                                             ⇡
	//  Index = 13  
	$task->fibonacciIndex(233);
	// Test C
	// number = 21    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                       ⇡
	//  Index = 8  
	$task->fibonacciIndex(21);
}
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()
{
	var task = new FibonacciNumber();
	// Test A
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                             ⇡
	//  Index = 5              
	task.fibonacciIndex(5);
	// Test B
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                                             ⇡
	//  Index = 13  
	task.fibonacciIndex(233);
	// Test C
	// number = 21    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                       ⇡
	//  Index = 8  
	task.fibonacciIndex(21);
}
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() :
	task = FibonacciNumber()
	#  Test A
	#  number = 5    
	#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	#                              ⇡
	#   Index = 5              
	task.fibonacciIndex(5)
	#  Test B
	#  number = 5    
	#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	#                                                              ⇡
	#   Index = 13  
	task.fibonacciIndex(233)
	#  Test C
	#  number = 21    
	#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	#                                        ⇡
	#   Index = 8  
	task.fibonacciIndex(21)

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() 
	task = FibonacciNumber.new()
	#  Test A
	#  number = 5    
	#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	#                              ⇡
	#   Index = 5              
	task.fibonacciIndex(5)
	#  Test B
	#  number = 5    
	#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	#                                                              ⇡
	#   Index = 13  
	task.fibonacciIndex(233)
	#  Test C
	#  number = 21    
	#  fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	#                                        ⇡
	#   Index = 8  
	task.fibonacciIndex(21)
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              
		task.fibonacciIndex(5);
		// Test B
		// number = 5    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                                                             ⇡
		//  Index = 13  
		task.fibonacciIndex(233);
		// Test C
		// number = 21    
		// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
		//                                       ⇡
		//  Index = 8  
		task.fibonacciIndex(21);
	}
}

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()
{
	let task: FibonacciNumber = FibonacciNumber();
	// Test A
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                             ⇡
	//  Index = 5              
	task.fibonacciIndex(5);
	// Test B
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                                             ⇡
	//  Index = 13  
	task.fibonacciIndex(233);
	// Test C
	// number = 21    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                       ⇡
	//  Index = 8  
	task.fibonacciIndex(21);
}
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
{
	val task: FibonacciNumber = FibonacciNumber();
	// Test A
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                             ⇡
	//  Index = 5              
	task.fibonacciIndex(5);
	// Test B
	// number = 5    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                                             ⇡
	//  Index = 13  
	task.fibonacciIndex(233);
	// Test C
	// number = 21    
	// fibonacci : [0  1  1  2  3  5  8  13  21  34  55  89  144  233  377..]
	//                                       ⇡
	//  Index = 8  
	task.fibonacciIndex(21);
}

Output

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


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.

New Comment







© 2021, kalkicode.com, All rights reserved