You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java
+18-1
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,7 @@ public static void main(String[] args) {
19
19
System.out.println(fibMemo(n));
20
20
System.out.println(fibBotUp(n));
21
21
System.out.println(fibOptimized(n));
22
+
System.out.println(fibBinet(n));
22
23
sc.close();
23
24
}
24
25
@@ -90,5 +91,21 @@ public static int fibOptimized(int n) {
90
91
res = next;
91
92
}
92
93
returnres;
93
-
}
94
94
}
95
+
96
+
/**
97
+
* We have only defined the nth Fibonacci number in terms of the two before it. Now, we will look at Binet's formula to calculate the nth Fibonacci number in constant time.
98
+
* The Fibonacci terms maintain a ratio called golden ratio denoted by Φ, the Greek character pronounced ‘phi'.
99
+
* First, let's look at how the golden ratio is calculated: Φ = ( 1 + √5 )/2 = 1.6180339887...
100
+
* Now, let's look at Binet's formula: Sn = Φⁿ–(– Φ⁻ⁿ)/√5
101
+
* We first calculate the squareRootof5 and phi and store them in variables. Later, we apply Binet's formula to get the required term.
0 commit comments