Skip to content

Commit 016d94f

Browse files
committed
Added Optimized solution for computing nth term in fibonacci series
1 parent 4858ec9 commit 016d94f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.recursion;
2+
3+
/*
4+
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones,
5+
starting with 0 and 1.
6+
NUMBER 0 1 2 3 4 5 6 7 8 9 10 ...
7+
FIBONACCI 0 1 1 2 3 5 8 13 21 34 55 ...
8+
*/
9+
10+
/*T his is optimized solution for fibonacci series. It computes fibonacci series in linear(O(n)) time complexity */
11+
12+
public final class FibonacciOptimized {
13+
static int fibonacci(int limit, int num1, int num2){
14+
if(limit==1){
15+
return num1;
16+
}
17+
else{
18+
return fibonacci(limit-1, num2, num1+num2);
19+
}
20+
}
21+
22+
}
23+

0 commit comments

Comments
 (0)