# 定义

数学公式

F0=0F_{0} = 0

F1=1F_{1} = 1

Fn=Fn1+Fn2(n2)F_{n} = F_{n - 1} + F_{n - 2} (n \geq 2)

# 实现代码

/*
 * @Author: Rainy
 * @Date: 2020-01-27 13:38:10
 * @LastEditors  : Rainy
 * @LastEditTime : 2020-01-27 14:11:53
 */

import { NumberArrayMap } from 'types';

export function fibonacci(n: number): NumberArrayMap {
  const fibSequence: NumberArrayMap = [1];

  let currentValue = 1;
  let previousValue = 0;

  if (n === 1) {
    return fibSequence;
  }

  let counter = n - 1;

  while (counter--) {
    currentValue += previousValue;
    previousValue = currentValue - previousValue;

    fibSequence.push(currentValue);
  }

  return fibSequence;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 参考资料

斐波那契数列 - wiki (opens new window)