class Fibonacci { public static void main(String[] args){ int index = 4; //System.out.println(recur_fibo(index+1)); //System.out.println(memo_fibo(index+1)); System.out.println(series_fibo(index)); } public static long recur_fibo (int n){ if (n<2) return n; long ret = recur_fibo(n - 1) + recur_fibo(n - 2); return ret; }
public static long memo_fibo (int n){ long[] memo = new long[n+1]; if(memo[n] ==0){ if (n < 2) { memo[n] = n; } else { memo[n] = memo_fibo(n - 1) + memo_fibo(n - 2); } } return memo[n]; }
static int a=0,b=1,c=0; public static long series_fibo (int n){ if(n>0){ c = a + b; a= b; b= c; series_fibo(n-1); } return c; } }