EN
TypeScript - Iterative Fibonacci sequence / number algorithm
0 points
Using TypeScript it is possible to compute the Fibonacci sequence in the following way.
xxxxxxxxxx
1
const calculateValue = (input: number) => {
2
if (input < 1) {
3
return 0;
4
}
5
let a = 0;
6
let b = 1;
7
for (let i = 1; i < input; ++i) {
8
const c = a + b;
9
a = b;
10
b = c;
11
}
12
return b;
13
};
14
15
// Example:
16
17
for (let n = 0; n < 10; ++n) {
18
console.log('f(' + n + ')=' + calculateValue(n));
19
}
Output:
xxxxxxxxxx
1
f(0)=0
2
f(1)=1
3
f(2)=1
4
f(3)=2
5
f(4)=3
6
f(5)=5
7
f(6)=8
8
f(7)=13
9
f(8)=21
10
f(9)=34