Skip to content

斐波拉契数列I

斐波那契数列示例:

1,1,2,3,5,8,13,21,34 。。。。

实现这样一个函数,得到斐波拉契数列指定位置上的值

迭代

js
/**
 * 求斐波拉契数列第n项
 * @param {number} index 
 * @returns {number}
 */
function fib(index) {
    let a = 1, b = 1, c = 0
    if (index === 1 || index === 2) {
        return 1
    }
    while (index-- > 2) {
        c = a + b
        a = b
        b = c
    }
    return c
};
/**
 * 求斐波拉契数列第n项
 * @param {number} index 
 * @returns {number}
 */
function fib(index) {
    let a = 1, b = 1, c = 0
    if (index === 1 || index === 2) {
        return 1
    }
    while (index-- > 2) {
        c = a + b
        a = b
        b = c
    }
    return c
};

递归

js
/**
 * 求斐波拉契数列第n项
 * @param {number} index 
 * @returns {number}
 */
function fib(index) {
    if (index === 1 || index === 2) {
        return 1
    }
    return fib(index - 1) + fib(index - 2)
};
/**
 * 求斐波拉契数列第n项
 * @param {number} index 
 * @returns {number}
 */
function fib(index) {
    if (index === 1 || index === 2) {
        return 1
    }
    return fib(index - 1) + fib(index - 2)
};

更新于: