📜  斐波那契数 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:16.062000             🧑  作者: Mango

代码示例5
f(n) = f(n-1) + f(n-2) 
                                  f(6)
                                   ^
                                     /\
                f(5)               +                       f(4)
                ^
               /\                                           /\
                
        f(4)    +           f(3)                     f(3)    +    f(2)
       ^                       ^                     ^              ^
      /\                       /\                    /\            /\
   
 f(3)   +       f(2)            f(2) +f(1)       f(2) + f(1)   f(1) +  f(0)             
   ^              ^                ^                ^
   /\             /\                /\              /\
    
f(2) + f(1)      f(1) +  f(0)     f(1)+ f(0)       f(1) + f(0)         
  ^
  /\
f(1) +  f(0) 
  
//f(6) = 8   ==>  f(1)*8    f(1) appears 8 times 
 double feb  = (1/Math.pow(5,0.5)) * (Math.pow((1+Math.pow(5,0.5))/2,n)) - (1/Math.pow(5,0.5))* (Math.pow((1-Math.pow(5,0.5))/2,n));  
  
f(1) == 1;