Hi 👋, I'm a software engineer specializing in backend systems, distributed systems, and scalable architecture. My blog shares practical tutorials based on 3+ years of experience. LeetCode 1756 (Top 10%). Actively seeking SDE roles — let's get in touch!
Comments
Leave a Comment
Success!
Receive Latest Updates 📬
Get every new post, special offers, and more via email. No fee required.
In this tutorial, we will learn about Binet's formula, which is a quick and efficient way to calculate the n-th Fibonacci number.
By using this formula, no loop is required and can be useful when you want to directly find the n-th Fibonacci number in O(1) time complexity.
Binet (pronounced as BEE-nay or buh-NET) is a French mathematician who discovered a formula to calculate the n-th Fibonacci number called Binet's formula.
Binet's formula is a closed-form expression allowing us to directly calculate the n-th Fibonacci number without using iterative loops. The formula is as follows:
public class FibonacciCalculator { public static double calculateFibonacci(int n) { double phi = (1 + Math.sqrt(5)) / 2; return (Math.pow(phi, n) - Math.pow(-phi, -n)) / Math.sqrt(5); } public static void main(String[] args) { System.out.print("Enter the value of n: "); int n = new Scanner(System.in).nextInt(); double fibonacci = calculateFibonacci(n); System.out.println("The " + n + "-th Fibonacci number is: " + fibonacci); }}
The algorithm complexity of calculating the n-th Fibonacci number using Binet's formula is O(1), which means it has a constant time complexity. This is because the calculation involves only a few mathematical operations and does not depend on the value of n.
Comments