Given a number n, print n-th Fibonacci Number, using Binet's Formula.
Examples:
Input: n = 5
Output: 1
Explanation: The 5th Fibonacci number in the sequence (0, 1, 1, 2, 3, 5, ...) is 1.Input: n = 9
Output: 34
Explanation: The 9th Fibonacci number in the sequence (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...) is 34.
Binet’s Formula for Fibonacci Numbers
Binet's Formula provides a closed-form expression for the Fibonacci sequence:
Fn = (1 / √5) * (φ^n - ψ^n)
where:
- φ (phi) = (1 + √5) / 2 (Golden Ratio)
- ψ (psi) = (1 - √5) / 2 (Negative Reciprocal of φ)
Why Isn't Binet's Formula Used Regularly?
Although Binet’s Formula is mathematically correct, it is rarely used in practice due to floating-point precision errors. These errors arise because of the irrational numbers involved in the computation.
- The formula produces accurate results only up to n < 71 due to rounding errors.
- For n = 71, using the floor function instead of rounding provides the correct result.
- However, for n ≥ 72, even the floor function fails.
Example:
Correct value for F(72) = 498454011879264
Binet’s Formula computes 498454011879265 (incorrect due to precision loss).
Efficient Approach to Compute the nth Fibonacci Number
Instead of Binet’s Formula, iterative methods, matrix exponentiation, or fast doubling techniques are preferred for computing large Fibonacci numbers efficiently and accurately.
However, if using Binet’s Formula for small values of n, the following implementation can be used:
Fn = (1 / √5) * φ^n
where:
- φ (phi) = (1 + √5) / 2 (Golden Ratio)
For practical applications, integer-based methods are recommended to avoid precision errors.
Below is the implementation of the above approach:
// C++ Implementation Using Binet's Formula
#include <bits/stdc++.h>
using namespace std;
int fibonacci(int n) {
// Golden ratio (φ) and its negative counterpart (ψ)
double phi = (1 + sqrt(5)) / 2;
double psi = (1 - sqrt(5)) / 2;
// Compute Fibonacci number using full Binet's formula
return round((pow(phi, n) - pow(psi, n)) / sqrt(5));
}
int main() {
int n = 5; // Example input
cout << "Fibonacci(" << n << ") = " << fibonacci(n) << endl;
n = 9;
cout << "Fibonacci(" << n << ") = " << fibonacci(n) << endl;
return 0;
}
// Java Implementation Using Binet's Formula
class GfG {
public static int fibonacci(int n) {
// Golden ratio (φ) and its negative counterpart (ψ)
double phi = (1 + Math.sqrt(5)) / 2;
double psi = (1 - Math.sqrt(5)) / 2;
// Compute Fibonacci number using full Binet's formula
return (int) Math.round((Math.pow(phi, n) - Math.pow(psi, n)) / Math.sqrt(5));
}
public static void main(String[] args) {
int n = 5; // Example input
System.out.println("Fibonacci(" + n + ") = " + fibonacci(n));
n = 9;
System.out.println("Fibonacci(" + n + ") = " + fibonacci(n));
}
}
# Python Implementation Using Binet's Formula
import math
def fibonacci(n):
# Golden ratio (φ) and its negative counterpart (ψ)
phi = (1 + math.sqrt(5)) / 2
psi = (1 - math.sqrt(5)) / 2
# Compute Fibonacci number using full Binet's formula
return round((math.pow(phi, n) - math.pow(psi, n)) / math.sqrt(5))
if __name__ == "__main__":
n = 5 # Example input
print(f"Fibonacci({n}) =", fibonacci(n))
n = 9
print(f"Fibonacci({n}) =", fibonacci(n))
// C# Implementation Using Binet's Formula
using System;
class GfG {
static int Fibonacci(int n) {
// Golden ratio (φ) and its negative counterpart (ψ)
double phi = (1 + Math.Sqrt(5)) / 2;
double psi = (1 - Math.Sqrt(5)) / 2;
// Compute Fibonacci number using full Binet's formula
return (int)Math.Round((Math.Pow(phi, n) - Math.Pow(psi, n)) / Math.Sqrt(5));
}
static void Main() {
int n = 5; // Example input
Console.WriteLine("Fibonacci(" + n + ") = " + Fibonacci(n));
n = 9;
Console.WriteLine("Fibonacci(" + n + ") = " + Fibonacci(n));
}
}
// JavaScript Implementation Using Binet's Formula
function fibonacci(n) {
// Golden ratio (φ) and its negative counterpart (ψ)
const phi = (1 + Math.sqrt(5)) / 2;
const psi = (1 - Math.sqrt(5)) / 2;
// Compute Fibonacci number using full Binet's formula
return Math.round((Math.pow(phi, n) - Math.pow(psi, n)) / Math.sqrt(5));
}
// Example usage
let n = 5;
console.log(`Fibonacci(${n}) = ${fibonacci(n)}`);
n = 9;
console.log(`Fibonacci(${n}) = ${fibonacci(n)}`);
Output
Fibonacci(5) = 5 Fibonacci(9) = 34
Time Complexity: O(log n), this is due to the pow function, which uses exponentiation by squaring.
Auxiliary Space: O(1), as only a few variables are used, with no extra data structures.