File tree Expand file tree Collapse file tree 1 file changed +6
-3
lines changed
src/com/codefortomorrow/advanced/chapter13/examples Expand file tree Collapse file tree 1 file changed +6
-3
lines changed Original file line number Diff line number Diff line change 11package com .codefortomorrow .advanced .chapter13 .examples ;
22
33public class Fibonacci {
4+
45 public static int fibonacciRecursion (int nthNumber ) {
56 //use recursion
67 if (nthNumber == 0 ) {
78 return 0 ;
89 } else if (nthNumber == 1 ) {
910 return 1 ;
1011 }
11- return fibonacciRecursion (nthNumber - 1 ) + fibonacciRecursion (nthNumber - 2 );
12+ return (
13+ fibonacciRecursion (nthNumber - 1 ) +
14+ fibonacciRecursion (nthNumber - 2 )
15+ );
1216 }
1317
1418 public static int fibonacciLoop (int nthNumber ) {
1519 //use loop
1620 int previouspreviousNumber , previousNumber = 0 , currentNumber = 1 ;
17- for (int i = 1 ; i < nthNumber ; i ++) {
21+ for (int i = 1 ; i < nthNumber ; i ++) {
1822 previouspreviousNumber = previousNumber ;
1923 previousNumber = currentNumber ;
2024 currentNumber = previouspreviousNumber + previousNumber ;
2125 }
2226 return currentNumber ;
2327 }
24-
2528 // Credit: https://dev.to/khalilsaboor/fibonacci-recursion-vs-iteration--474l
2629}
You can’t perform that action at this time.
0 commit comments