| 2 | class Fibonacci // if asked for a term always use bennet's formula, open purple register |
| 3 | { |
| 4 | static void nth_fibo(int b, int c, int n) //better than f(n-1) + f(n-2), coz here a lot |
| 5 | { //overlapping subproblems occur |
| 6 | /* |
| 7 | if(n < 0) |
| 8 | return 0; |
| 9 | if(n < 2) |
| 10 | return n; |
| 11 | return(fibo(n-1) + fibo(n-2)); //tradional 2^n solution |
| 12 | */ |
| 13 | if (n == 2) //if u say 0 is the first term, else take n == 1 |
| 14 | { |
| 15 | System.out.println(c); |
| 16 | return; |
| 17 | } |
| 18 | //Uncomment to print the series |
| 19 | //System.out.print(c+b + " "); |
| 20 | nth_fibo(c, c + b, n - 1); //To print use System.out.println(c+b), above this statement |
| 21 | } |
| 22 | |
| 23 | public static void main(String args[]) { |
| 24 | Scanner I = new Scanner(System.in); |