斐波那契非递归

实现代码

Posted by Lan on July 19, 2020

斐波那契数列-非递归实现

function f(n){
  if(n==0)return 0  
  if(n==1||n==2){
     return 1
   }
    let a=1,b=1,c=0
    
   
     while(n>2){
         c=a+b
         a=b
         b=c
         n--
      }
     return c
    
}
console.log(f(1))//1
console.log(f(2))//1
console.log(f(3))//2
console.log(f(4))//3
console.log(f(5))//5