java - Need to explain with Fibonacci -


i have seen many examples of fibonacci here on stack overflow have found no answer question. so, have code:

public class fib {     public static int fib(int n) {         if (n < 2) {            return n;         }         else {    return fib(n-1)+fib(n-2);         } }  public static void main(string[] args) {     (int i=0; i<8; i++)         system.out.print(fib(i)+", "); } } 

after run 0, 1, 1, 2, 3, 5, 8, 13,

i have question: how 8 ? fib(6)=............

can write in detail?

fib(6) = fib(5)+fib(4) = fib(4)+fib(3)+fib(3)+fib(2) = fib(3)+fib(2)+fib(2)+fib(1)+fib(2)+fib(1)+fib(1)+fib(0) = fib(2)+fib(1)+fib(1)+fib(0)+fib(1)+fib(0)+1+fib(1)+fib(0)+1+1+0 = fib(1)+fib(0)+1+1+0+1+0+1+1+0+1+1+0 = 1+0+1+1+0+1+0+1+1+0+1+1+0=8 

hope helped..


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -