首页 > 代码库 > 斐波拉契数列求第N个;

斐波拉契数列求第N个;

递归写法:

function fblq(a){        if(a == 1 || a == 2){            return 1;        }else{            return fblq(a -1) + fblq(a -2);        }    }alert(fblq(6));

非递归:

function fibonacci(n){            var a,b,res;            a = b = 1;            for(var i=3;i<=n;i++){                res = a + b;                a = b;                b = res;            }            return res;        }        alert(fibonacci(6));

 

斐波拉契数列求第N个;