# 定义
数学公式
# 实现代码
/*
* @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
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