MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / Fibonacci

Class Fibonacci

Java/Recursion/Fibonacci.java:2–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.*;
2class 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);
25 System.out.println("Enter the number n, to get the nth fibonacci number");
26 int n = I.nextInt();
27 if (n < 3)
28 System.out.println(n - 1);
29 else
30 {
31 System.out.println("0 1 ");
32 nth_fibo(0, 1, n);
33 }
34 I.close();
35 }
36}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected