Skip to content

Commit 33df079

Browse files
authored
Added negatives support to every fn
1 parent ea39661 commit 33df079

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

Maths/Fibonacci.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
const list = []
2-
2+
// https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers
33
const FibonacciIterative = (nth) => {
4-
const sequence = []
4+
const sign = nth < 0
5+
if (sign) n = -n
6+
const sequence = [0]
57

68
if (nth >= 1) sequence.push(1)
7-
if (nth >= 2) sequence.push(1)
9+
if (nth >= 2) sequence.push(sign ? -1 : 1)
810

911
for (let i = 2; i < nth; i++) {
10-
sequence.push(sequence[i - 1] + sequence[i - 2])
12+
sequence.push(
13+
sign ?
14+
sequence[i - 2] - sequence[i - 1]
15+
:
16+
sequence[i - 1] + sequence[i - 2]
17+
)
1118
}
1219

1320
return sequence
@@ -17,15 +24,21 @@ const FibonacciRecursive = (number) => {
1724
return (() => {
1825
switch (list.length) {
1926
case 0:
20-
list.push(1)
27+
list.push(0)
2128
return FibonacciRecursive(number)
2229
case 1:
2330
list.push(1)
2431
return FibonacciRecursive(number)
2532
case number:
2633
return list
2734
default:
28-
list.push(list[list.length - 1] + list[list.length - 2])
35+
const sign = number < 0
36+
list.push(
37+
sign ?
38+
list.at(-2) - list.at(-1)
39+
:
40+
list.at(-1) + list.at(-2)
41+
)
2942
return FibonacciRecursive(number)
3043
}
3144
})()
@@ -34,14 +47,18 @@ const FibonacciRecursive = (number) => {
3447
const dict = new Map()
3548

3649
const FibonacciRecursiveDP = (stairs) => {
37-
if (stairs <= 0) return 0
50+
const sign = stairs < 0
51+
if (sign) stairs *= -1
52+
53+
if (stairs === 0) return 0
3854
if (stairs === 1) return 1
3955

4056
// Memoize stair count
4157
if (dict.has(stairs)) return dict.get(stairs)
4258

43-
const res =
44-
FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2)
59+
const res = sign
60+
? FibonacciRecursiveDP(stairs - 2) - FibonacciRecursiveDP(stairs - 1)
61+
: FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2)
4562

4663
dict.set(stairs, res)
4764

@@ -60,11 +77,18 @@ const FibonacciRecursiveDP = (stairs) => {
6077
// @Satzyakiz
6178

6279
const FibonacciDpWithoutRecursion = (number) => {
63-
const table = []
64-
table.push(1)
80+
const sgn = number < 0
81+
if (sgn) number *= -1
82+
const table = [0]
6583
table.push(1)
84+
table.push(sgn ? -1 : 1)
6685
for (let i = 2; i < number; ++i) {
67-
table.push(table[i - 1] + table[i - 2])
86+
table.push(
87+
sgn ?
88+
table[i - 2] - table[i - 1]
89+
:
90+
table[i - 1] + table[i - 2]
91+
)
6892
}
6993
return table
7094
}
@@ -75,10 +99,11 @@ const copyMatrix = (A) => {
7599
return A.map(row => row.map(cell => cell))
76100
}
77101

78-
// the 2nd param is to generate a "BigInt-safe" matrix
79-
const Identity = (size, bigint) => {
80-
const ZERO = bigint ? 0n : 0
81-
const ONE = bigint ? 1n : 1
102+
const Identity = (size) => {
103+
const isBigInt = typeof size === 'bigint'
104+
const ZERO = isBigInt ? 0n : 0
105+
const ONE = isBigInt ? 1n : 1
106+
size = Number(size)
82107
const I = Array(size).fill(null).map(() => Array(size).fill())
83108
return I.map((row, rowIdx) => row.map((_col, colIdx) => {
84109
return rowIdx === colIdx ? ONE : ZERO
@@ -164,7 +189,6 @@ const FibonacciMatrixExpo = (n) => {
164189
[ZERO]
165190
]
166191
F = matrixMultiply(poweredA, F)
167-
// https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers
168192
return F[0][0] * (sign ? (-ONE) ** (n + ONE) : ONE)
169193
}
170194

0 commit comments

Comments
 (0)