Skip to content

Commit cdff2a8

Browse files
committed
fix: use bigint type
1 parent e9b417f commit cdff2a8

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/algorithms/math/fibonacci/fibonacci-sequence-floop.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Time complexity: O(n)
44
* Space complexity: O(n)
55
* @param {number} length Length of the Fibonacci sequence.
6-
* @returns {number[]} Fibonacci sequence of the specified length.
6+
* @returns {bigint[]} Fibonacci sequence of the specified length.
77
*/
88
const fibonacciSequence = (length) => {
99
if (length === 0) return []
@@ -12,7 +12,9 @@ const fibonacciSequence = (length) => {
1212
const sequence = [0n, 1n]
1313

1414
for (let index = 2; index < length; index++) {
15-
sequence.push(sequence.at(-1) + sequence.at(-2))
15+
const idxMinOne = sequence.at(-1) ?? 0n
16+
const idxMinTwo = sequence.at(-2) ?? 0n
17+
sequence.push(idxMinOne + idxMinTwo)
1618
}
1719

1820
return sequence

src/algorithms/math/fibonacci/fibonacci-sequence-reduce.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
* Time complexity: O(n)
44
* Space complexity: O(n)
55
* @param {number} length Length of the Fibonacci sequence.
6-
* @returns {number[]} Fibonacci sequence of the specified length.
6+
* @returns {bigint[]} Fibonacci sequence of the specified length.
77
*/
88
const fibonacciSequence = (length) => {
99
const fibos = [0n, 1n]
1010
if (length < 2) return fibos.slice(0, length)
1111

1212
return [...Array(length - 2).keys()].reduce(fibos => {
13-
fibos.push(fibos.at(-1) + fibos.at(-2))
13+
const idxMinOne = fibos.at(-1) ?? 0n
14+
const idxMinTwo = fibos.at(-2) ?? 0n
15+
fibos.push(idxMinOne + idxMinTwo)
1416
return fibos
1517
}, fibos)
1618
}

src/algorithms/math/fibonacci/fibonacci-sequence-rmemo.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* Time complexity: O(n)
44
* Space complexity: O(n)
55
* @param {number} length Length of the Fibonacci sequence.
6-
* @returns {number[]} Fibonacci sequence of the specified length.
6+
* @returns {bigint[]} Fibonacci sequence of the specified length.
77
*/
88
const fibonacciSequence = (length) => {
99
if (length === 0) return []
1010
if (length === 1) return [0n]
1111

1212
const sequence = [0n, 1n]
1313

14+
/** @param {number} n */
1415
const fibo = (n) => {
1516
if (typeof sequence[n] !== 'undefined') return sequence[n]
1617
sequence[n] = fibo(n - 1) + fibo(n - 2)

0 commit comments

Comments
 (0)