From 398854c641d479ef7ff37bfdbd1187610bd6f14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 13:35:42 -0400 Subject: [PATCH 01/25] Added BigInt support to `FibonacciMatrixExpo` Also added support to `Identity`, `matrixMultiply`, and `matrixExpo` --- Maths/Fibonacci.js | 49 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index c1b153f24d..6ff1f0380b 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -75,25 +75,31 @@ const copyMatrix = (A) => { return A.map(row => row.map(cell => cell)) } -const Identity = (size) => { +// the 2nd param is to generate a "BigInt-safe" matrix +const Identity = (size, bigint) => { + const ZERO = bigint ? 0n : 0 + const ONE = bigint ? 1n : 1 const I = Array(size).fill(null).map(() => Array(size).fill()) return I.map((row, rowIdx) => row.map((_col, colIdx) => { - return rowIdx === colIdx ? 1 : 0 + return rowIdx === colIdx ? ONE : ZERO })) } // A of size (l x m) and B of size (m x n) -// product C will be of size (l x n) +// product C will be of size (l x n). +// both matrices must have same-type numeric values +// either both BigInt or both Number const matrixMultiply = (A, B) => { A = copyMatrix(A) B = copyMatrix(B) + const isBigInt = typeof A[0][0] === "bigint" const l = A.length const m = B.length const n = B[0].length // Assuming non-empty matrices const C = Array(l).fill(null).map(() => Array(n).fill()) for (let i = 0; i < l; i++) { for (let j = 0; j < n; j++) { - C[i][j] = 0 + C[i][j] = isBigInt ? 0n : 0 for (let k = 0; k < m; k++) { C[i][j] += A[i][k] * B[k][j] } @@ -110,13 +116,17 @@ const matrixMultiply = (A, B) => { // A is a square matrix const matrixExpo = (A, n) => { A = copyMatrix(A) + const isBigInt = typeof n === "bigint" + const ZERO = isBigInt ? 0n : 0 + const TWO = isBigInt ? 2n : 2 // Just like Binary exponentiation mentioned in ./BinaryExponentiationIterative.js let result = Identity(A.length) // Identity matrix - while (n > 0) { - if (n % 2 !== 0) result = matrixMultiply(result, A) - n = Math.floor(n / 2) - if (n > 0) A = matrixMultiply(A, A) + while (n > ZERO) { + if (n % TWO !== ZERO) result = matrixMultiply(result, A) + n /= TWO + if (!isBigInt) n = Math.floor(n) + if (n > ZERO) A = matrixMultiply(A, A) } return result } @@ -134,19 +144,28 @@ const FibonacciMatrixExpo = (n) => { // or F(n, n-1) = A * A * F(n-2, n-3) // or F(n, n-1) = pow(A, n-1) * F(1, 0) - if (n === 0) return 0 + if (n === 0 || n === 0n) return n + + let sign = n < 0 + if (sign) n = -n + + const isBigInt = typeof n === "bigint" + const ZERO = isBigInt ? 0n : 0 + const ONE = isBigInt ? 1n : 1 const A = [ - [1, 1], - [1, 0] + [ONE, ONE], + [ONE, ZERO] ] - const poweredA = matrixExpo(A, n - 1) // A raised to the power n-1 + + const poweredA = matrixExpo(A, n - ONE) // A raised to the power n-1 let F = [ - [1], - [0] + [ONE], + [ZERO] ] F = matrixMultiply(poweredA, F) - return F[0][0] + // https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers + return F[0][0] * (sign ? (-ONE)**(n + ONE) : ONE) } export { FibonacciDpWithoutRecursion } From ea3966108dc4e1373e94cbc6ba0a3c7174fbb5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 13:44:09 -0400 Subject: [PATCH 02/25] Fixed style --- Maths/Fibonacci.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 6ff1f0380b..8e51a7809c 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -92,7 +92,7 @@ const Identity = (size, bigint) => { const matrixMultiply = (A, B) => { A = copyMatrix(A) B = copyMatrix(B) - const isBigInt = typeof A[0][0] === "bigint" + const isBigInt = typeof A[0][0] === 'bigint' const l = A.length const m = B.length const n = B[0].length // Assuming non-empty matrices @@ -116,7 +116,7 @@ const matrixMultiply = (A, B) => { // A is a square matrix const matrixExpo = (A, n) => { A = copyMatrix(A) - const isBigInt = typeof n === "bigint" + const isBigInt = typeof n === 'bigint' const ZERO = isBigInt ? 0n : 0 const TWO = isBigInt ? 2n : 2 @@ -146,10 +146,10 @@ const FibonacciMatrixExpo = (n) => { if (n === 0 || n === 0n) return n - let sign = n < 0 + const sign = n < 0 if (sign) n = -n - const isBigInt = typeof n === "bigint" + const isBigInt = typeof n === 'bigint' const ZERO = isBigInt ? 0n : 0 const ONE = isBigInt ? 1n : 1 @@ -165,7 +165,7 @@ const FibonacciMatrixExpo = (n) => { ] F = matrixMultiply(poweredA, F) // https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers - return F[0][0] * (sign ? (-ONE)**(n + ONE) : ONE) + return F[0][0] * (sign ? (-ONE) ** (n + ONE) : ONE) } export { FibonacciDpWithoutRecursion } From 33df07970b27dcb77574212ff93b69305bf29bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:10:51 -0400 Subject: [PATCH 03/25] Added negatives support to every fn --- Maths/Fibonacci.js | 58 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 8e51a7809c..1c1b0674b5 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -1,13 +1,20 @@ const list = [] - +// https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers const FibonacciIterative = (nth) => { - const sequence = [] + const sign = nth < 0 + if (sign) n = -n + const sequence = [0] if (nth >= 1) sequence.push(1) - if (nth >= 2) sequence.push(1) + if (nth >= 2) sequence.push(sign ? -1 : 1) for (let i = 2; i < nth; i++) { - sequence.push(sequence[i - 1] + sequence[i - 2]) + sequence.push( + sign ? + sequence[i - 2] - sequence[i - 1] + : + sequence[i - 1] + sequence[i - 2] + ) } return sequence @@ -17,7 +24,7 @@ const FibonacciRecursive = (number) => { return (() => { switch (list.length) { case 0: - list.push(1) + list.push(0) return FibonacciRecursive(number) case 1: list.push(1) @@ -25,7 +32,13 @@ const FibonacciRecursive = (number) => { case number: return list default: - list.push(list[list.length - 1] + list[list.length - 2]) + const sign = number < 0 + list.push( + sign ? + list.at(-2) - list.at(-1) + : + list.at(-1) + list.at(-2) + ) return FibonacciRecursive(number) } })() @@ -34,14 +47,18 @@ const FibonacciRecursive = (number) => { const dict = new Map() const FibonacciRecursiveDP = (stairs) => { - if (stairs <= 0) return 0 + const sign = stairs < 0 + if (sign) stairs *= -1 + + if (stairs === 0) return 0 if (stairs === 1) return 1 // Memoize stair count if (dict.has(stairs)) return dict.get(stairs) - const res = - FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) + const res = sign + ? FibonacciRecursiveDP(stairs - 2) - FibonacciRecursiveDP(stairs - 1) + : FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) dict.set(stairs, res) @@ -60,11 +77,18 @@ const FibonacciRecursiveDP = (stairs) => { // @Satzyakiz const FibonacciDpWithoutRecursion = (number) => { - const table = [] - table.push(1) + const sgn = number < 0 + if (sgn) number *= -1 + const table = [0] table.push(1) + table.push(sgn ? -1 : 1) for (let i = 2; i < number; ++i) { - table.push(table[i - 1] + table[i - 2]) + table.push( + sgn ? + table[i - 2] - table[i - 1] + : + table[i - 1] + table[i - 2] + ) } return table } @@ -75,10 +99,11 @@ const copyMatrix = (A) => { return A.map(row => row.map(cell => cell)) } -// the 2nd param is to generate a "BigInt-safe" matrix -const Identity = (size, bigint) => { - const ZERO = bigint ? 0n : 0 - const ONE = bigint ? 1n : 1 +const Identity = (size) => { + const isBigInt = typeof size === 'bigint' + const ZERO = isBigInt ? 0n : 0 + const ONE = isBigInt ? 1n : 1 + size = Number(size) const I = Array(size).fill(null).map(() => Array(size).fill()) return I.map((row, rowIdx) => row.map((_col, colIdx) => { return rowIdx === colIdx ? ONE : ZERO @@ -164,7 +189,6 @@ const FibonacciMatrixExpo = (n) => { [ZERO] ] F = matrixMultiply(poweredA, F) - // https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers return F[0][0] * (sign ? (-ONE) ** (n + ONE) : ONE) } From f4f1d5328855d06a87b0820584276a7bb464f377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:16:25 -0400 Subject: [PATCH 04/25] Fixed bug --- Maths/Fibonacci.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 1c1b0674b5..5d699aa3e0 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -146,7 +146,7 @@ const matrixExpo = (A, n) => { const TWO = isBigInt ? 2n : 2 // Just like Binary exponentiation mentioned in ./BinaryExponentiationIterative.js - let result = Identity(A.length) // Identity matrix + let result = Identity((isBigInt ? BigInt : Number)(A.length)) // Identity matrix while (n > ZERO) { if (n % TWO !== ZERO) result = matrixMultiply(result, A) n /= TWO From 2bcbc58898825be6814951cc43669c48baaa49be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:25:45 -0400 Subject: [PATCH 05/25] Added all missing tests --- Maths/test/Fibonacci.test.js | 47 +++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index bccd799be9..dfea5a603c 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -8,24 +8,34 @@ import { describe('Fibonacci', () => { it('should return an array of numbers for FibonacciIterative', () => { - expect(FibonacciIterative(5)).toEqual( - expect.arrayContaining([1, 1, 2, 3, 5]) + expect(FibonacciIterative(6)).toEqual( + expect.arrayContaining([0, 1, 1, 2, 3, 5, 8]) + ) + expect(FibonacciIterative(-6)).toEqual( + expect.arrayContaining([0, 1, -1, 2, -3, 5, -8]) ) }) it('should return an array of numbers for FibonacciRecursive', () => { - expect(FibonacciRecursive(5)).toEqual( - expect.arrayContaining([1, 1, 2, 3, 5]) + expect(FibonacciRecursive(6)).toEqual( + expect.arrayContaining([0, 1, 1, 2, 3, 5, 8]) + ) + expect(FibonacciRecursive(-6)).toEqual( + expect.arrayContaining([0, 1, -1, 2, -3, 5, -8]) ) }) it('should return number for FibonacciRecursiveDP', () => { expect(FibonacciRecursiveDP(5)).toBe(5) + expect(FibonacciRecursiveDP(-6)).toBe(-8) }) it('should return an array of numbers for FibonacciDpWithoutRecursion', () => { - expect(FibonacciDpWithoutRecursion(5)).toEqual( - expect.arrayContaining([1, 1, 2, 3, 5]) + expect(FibonacciDpWithoutRecursion(6)).toEqual( + expect.arrayContaining([0, 1, 1, 2, 3, 5, 8]) + ) + expect(FibonacciDpWithoutRecursion(-6)).toEqual( + expect.arrayContaining([0, 1, -1, 2, -3, 5, -8]) ) }) @@ -36,5 +46,30 @@ describe('Fibonacci', () => { expect(FibonacciMatrixExpo(3)).toBe(2) expect(FibonacciMatrixExpo(4)).toBe(3) expect(FibonacciMatrixExpo(5)).toBe(5) + expect(FibonacciMatrixExpo(6)).toBe(8) + + expect(FibonacciMatrixExpo(0n)).toBe(0n) + expect(FibonacciMatrixExpo(1n)).toBe(1n) + expect(FibonacciMatrixExpo(2n)).toBe(1n) + expect(FibonacciMatrixExpo(3n)).toBe(2n) + expect(FibonacciMatrixExpo(4n)).toBe(3n) + expect(FibonacciMatrixExpo(5n)).toBe(5n) + expect(FibonacciMatrixExpo(6n)).toBe(8n) + + expect(FibonacciMatrixExpo(-0)).toBe(-0) + expect(FibonacciMatrixExpo(-1)).toBe(1) + expect(FibonacciMatrixExpo(-2)).toBe(-1) + expect(FibonacciMatrixExpo(-3)).toBe(2) + expect(FibonacciMatrixExpo(-4)).toBe(-3) + expect(FibonacciMatrixExpo(-5)).toBe(5) + expect(FibonacciMatrixExpo(-6)).toBe(-8) + + expect(FibonacciMatrixExpo(-0n)).toBe(0n) + expect(FibonacciMatrixExpo(-1n)).toBe(1n) + expect(FibonacciMatrixExpo(-2n)).toBe(-1n) + expect(FibonacciMatrixExpo(-3n)).toBe(2n) + expect(FibonacciMatrixExpo(-4n)).toBe(-3n) + expect(FibonacciMatrixExpo(-5n)).toBe(5n) + expect(FibonacciMatrixExpo(-6n)).toBe(-8n) }) }) From 3d865f3bf1d11615a11da8159030b66f249f806c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:30:15 -0400 Subject: [PATCH 06/25] Splitted `BigInt` & `Number` tests --- Maths/test/Fibonacci.test.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index dfea5a603c..0812de4544 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -47,15 +47,7 @@ describe('Fibonacci', () => { expect(FibonacciMatrixExpo(4)).toBe(3) expect(FibonacciMatrixExpo(5)).toBe(5) expect(FibonacciMatrixExpo(6)).toBe(8) - - expect(FibonacciMatrixExpo(0n)).toBe(0n) - expect(FibonacciMatrixExpo(1n)).toBe(1n) - expect(FibonacciMatrixExpo(2n)).toBe(1n) - expect(FibonacciMatrixExpo(3n)).toBe(2n) - expect(FibonacciMatrixExpo(4n)).toBe(3n) - expect(FibonacciMatrixExpo(5n)).toBe(5n) - expect(FibonacciMatrixExpo(6n)).toBe(8n) - + expect(FibonacciMatrixExpo(-0)).toBe(-0) expect(FibonacciMatrixExpo(-1)).toBe(1) expect(FibonacciMatrixExpo(-2)).toBe(-1) @@ -63,7 +55,17 @@ describe('Fibonacci', () => { expect(FibonacciMatrixExpo(-4)).toBe(-3) expect(FibonacciMatrixExpo(-5)).toBe(5) expect(FibonacciMatrixExpo(-6)).toBe(-8) - + }) + + it('should return bigint for FibonacciMatrixExpo', () => { + expect(FibonacciMatrixExpo(0n)).toBe(0n) + expect(FibonacciMatrixExpo(1n)).toBe(1n) + expect(FibonacciMatrixExpo(2n)).toBe(1n) + expect(FibonacciMatrixExpo(3n)).toBe(2n) + expect(FibonacciMatrixExpo(4n)).toBe(3n) + expect(FibonacciMatrixExpo(5n)).toBe(5n) + expect(FibonacciMatrixExpo(6n)).toBe(8n) + expect(FibonacciMatrixExpo(-0n)).toBe(0n) expect(FibonacciMatrixExpo(-1n)).toBe(1n) expect(FibonacciMatrixExpo(-2n)).toBe(-1n) From 0dc81a8fa0efb8a18f1a291e4170f8b3c6352586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:37:22 -0400 Subject: [PATCH 07/25] Shortened `sign` to `sgn` in most (not all) places --- Maths/Fibonacci.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 5d699aa3e0..314c9a2c40 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -32,9 +32,9 @@ const FibonacciRecursive = (number) => { case number: return list default: - const sign = number < 0 + const sgn = number < 0 list.push( - sign ? + sgn ? list.at(-2) - list.at(-1) : list.at(-1) + list.at(-2) @@ -47,8 +47,8 @@ const FibonacciRecursive = (number) => { const dict = new Map() const FibonacciRecursiveDP = (stairs) => { - const sign = stairs < 0 - if (sign) stairs *= -1 + const sgn = stairs < 0 + if (sgn) stairs *= -1 if (stairs === 0) return 0 if (stairs === 1) return 1 @@ -56,7 +56,7 @@ const FibonacciRecursiveDP = (stairs) => { // Memoize stair count if (dict.has(stairs)) return dict.get(stairs) - const res = sign + const res = sgn ? FibonacciRecursiveDP(stairs - 2) - FibonacciRecursiveDP(stairs - 1) : FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) @@ -171,8 +171,8 @@ const FibonacciMatrixExpo = (n) => { if (n === 0 || n === 0n) return n - const sign = n < 0 - if (sign) n = -n + const sgn = n < 0 + if (sgn) n = -n const isBigInt = typeof n === 'bigint' const ZERO = isBigInt ? 0n : 0 @@ -189,7 +189,7 @@ const FibonacciMatrixExpo = (n) => { [ZERO] ] F = matrixMultiply(poweredA, F) - return F[0][0] * (sign ? (-ONE) ** (n + ONE) : ONE) + return F[0][0] * (sgn ? (-ONE) ** (n + ONE) : ONE) } export { FibonacciDpWithoutRecursion } From 1f61f5173b36b2a612637ca7944a61e7f100fe68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:48:13 -0400 Subject: [PATCH 08/25] Fixed indexing --- Maths/Fibonacci.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 314c9a2c40..406c1babde 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -29,7 +29,7 @@ const FibonacciRecursive = (number) => { case 1: list.push(1) return FibonacciRecursive(number) - case number: + case number + 1: return list default: const sgn = number < 0 From 7090d5aca46bb38923913ecd00d1d240cfc9381f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:00:15 -0400 Subject: [PATCH 09/25] Fixed bug at FDPWR --- Maths/Fibonacci.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 406c1babde..0def7b7827 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -85,9 +85,9 @@ const FibonacciDpWithoutRecursion = (number) => { for (let i = 2; i < number; ++i) { table.push( sgn ? - table[i - 2] - table[i - 1] + table[i - 1] - table[i] : - table[i - 1] + table[i - 2] + table[i] + table[i - 1] ) } return table From 03ed1f823e1516d93cd6e6705495a786a6f8abbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:01:19 -0400 Subject: [PATCH 10/25] Fixed FRDP --- Maths/Fibonacci.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 0def7b7827..55691cf96e 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -57,8 +57,8 @@ const FibonacciRecursiveDP = (stairs) => { if (dict.has(stairs)) return dict.get(stairs) const res = sgn - ? FibonacciRecursiveDP(stairs - 2) - FibonacciRecursiveDP(stairs - 1) - : FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) + ? FibonacciRecursiveDP(stairs - 1) - FibonacciRecursiveDP(stairs) + : FibonacciRecursiveDP(stairs) + FibonacciRecursiveDP(stairs - 1) dict.set(stairs, res) From b2101fcbd9dd6e55e92e2529dbeab28c5c6fcb5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:02:34 -0400 Subject: [PATCH 11/25] Fixed F iter --- Maths/Fibonacci.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 55691cf96e..dd20b3fb6e 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -11,9 +11,9 @@ const FibonacciIterative = (nth) => { for (let i = 2; i < nth; i++) { sequence.push( sign ? - sequence[i - 2] - sequence[i - 1] + sequence[i - 1] - sequence[i] : - sequence[i - 1] + sequence[i - 2] + sequence[i] + sequence[i - 1] ) } From c5fe0b5202dc23dad9166aa7a78b95973eca8466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:14:53 -0400 Subject: [PATCH 12/25] Fixed FRDP again --- Maths/Fibonacci.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index dd20b3fb6e..ade9920e16 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -50,15 +50,14 @@ const FibonacciRecursiveDP = (stairs) => { const sgn = stairs < 0 if (sgn) stairs *= -1 - if (stairs === 0) return 0 - if (stairs === 1) return 1 + if (stairs <= 1) return stairs // Memoize stair count if (dict.has(stairs)) return dict.get(stairs) const res = sgn - ? FibonacciRecursiveDP(stairs - 1) - FibonacciRecursiveDP(stairs) - : FibonacciRecursiveDP(stairs) + FibonacciRecursiveDP(stairs - 1) + ? FibonacciRecursiveDP(stairs - 2) - FibonacciRecursiveDP(stairs - 1) + : FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) dict.set(stairs, res) From 787fd75680b8ebbfb9db930e6126d76bf3ee2687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:39:38 -0400 Subject: [PATCH 13/25] Added `FibonacciGenerator` This wasn't originally part of the PR, but I thought it would be good to add --- Maths/Fibonacci.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index ade9920e16..7dc6d7e4dd 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -1,4 +1,3 @@ -const list = [] // https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers const FibonacciIterative = (nth) => { const sign = nth < 0 @@ -20,7 +19,20 @@ const FibonacciIterative = (nth) => { return sequence } +const FibonacciGenerator = function* (sign) { + let a = 0 + let b = 1 + yield a + while (true) { + yield b + [a, b] = sign ? [b, a - b] : [b, a + b] + } +} + +const list = [] const FibonacciRecursive = (number) => { + const sgn = number < 0 + if (sgn) number *= -1 return (() => { switch (list.length) { case 0: @@ -32,7 +44,6 @@ const FibonacciRecursive = (number) => { case number + 1: return list default: - const sgn = number < 0 list.push( sgn ? list.at(-2) - list.at(-1) @@ -45,7 +56,6 @@ const FibonacciRecursive = (number) => { } const dict = new Map() - const FibonacciRecursiveDP = (stairs) => { const sgn = stairs < 0 if (sgn) stairs *= -1 @@ -193,6 +203,7 @@ const FibonacciMatrixExpo = (n) => { export { FibonacciDpWithoutRecursion } export { FibonacciIterative } +export { FibonacciGenerator } export { FibonacciRecursive } export { FibonacciRecursiveDP } export { FibonacciMatrixExpo } From c11856bf635d26861a368d13fc6fb23f57da9282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:44:08 -0400 Subject: [PATCH 14/25] Added tests for `FibonacciGenerator` --- Maths/test/Fibonacci.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index 0812de4544..05d8d2e741 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -2,6 +2,7 @@ import { FibonacciDpWithoutRecursion, FibonacciRecursiveDP, FibonacciIterative, + FibonacciGenerator, FibonacciRecursive, FibonacciMatrixExpo } from '../Fibonacci' @@ -16,6 +17,26 @@ describe('Fibonacci', () => { ) }) + it('should return number for FibonacciGenerator', () => { + const positive = FibonacciGenerator() + expect(positive.next().value).toBe(0) + expect(positive.next().value).toBe(1) + expect(positive.next().value).toBe(1) + expect(positive.next().value).toBe(2) + expect(positive.next().value).toBe(3) + expect(positive.next().value).toBe(5) + expect(positive.next().value).toBe(8) + + const negative = FibonacciGenerator(true) + expect(negative.next().value).toBe(0) + expect(negative.next().value).toBe(1) + expect(negative.next().value).toBe(-1) + expect(negative.next().value).toBe(2) + expect(negative.next().value).toBe(-3) + expect(negative.next().value).toBe(5) + expect(negative.next().value).toBe(-8) + }) + it('should return an array of numbers for FibonacciRecursive', () => { expect(FibonacciRecursive(6)).toEqual( expect.arrayContaining([0, 1, 1, 2, 3, 5, 8]) From 028ebe9d046aa7bb519e015406c90c5a41721658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:44:56 -0400 Subject: [PATCH 15/25] More consistent test --- Maths/test/Fibonacci.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index 05d8d2e741..143e99401f 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -47,7 +47,7 @@ describe('Fibonacci', () => { }) it('should return number for FibonacciRecursiveDP', () => { - expect(FibonacciRecursiveDP(5)).toBe(5) + expect(FibonacciRecursiveDP(6)).toBe(8) expect(FibonacciRecursiveDP(-6)).toBe(-8) }) From 69bbe51cef0259cc23b7a9f629d69df594728927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:56:19 -0400 Subject: [PATCH 16/25] Fixed FRDP yet again --- Maths/Fibonacci.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 7dc6d7e4dd..99c195ac40 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -19,13 +19,13 @@ const FibonacciIterative = (nth) => { return sequence } -const FibonacciGenerator = function* (sign) { +const FibonacciGenerator = function* (negative) { let a = 0 let b = 1 yield a while (true) { yield b - [a, b] = sign ? [b, a - b] : [b, a + b] + [a, b] = negative ? [b, a - b] : [b, a + b] } } @@ -63,15 +63,13 @@ const FibonacciRecursiveDP = (stairs) => { if (stairs <= 1) return stairs // Memoize stair count - if (dict.has(stairs)) return dict.get(stairs) + if (dict.has(stairs)) return (sgn ? (-1) ** (stairs + 1) : 1) * dict.get(stairs) - const res = sgn - ? FibonacciRecursiveDP(stairs - 2) - FibonacciRecursiveDP(stairs - 1) - : FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) + const res = FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) dict.set(stairs, res) - return res + return (sgn ? (-1) ** (stairs + 1) : 1) * res } // Algorithms From 6f0de19f96154bf79bcf0123c30ee90c29aa941d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 16:06:15 -0400 Subject: [PATCH 17/25] Fixed Fib recursive --- Maths/Fibonacci.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 99c195ac40..bed7331bbe 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -44,15 +44,10 @@ const FibonacciRecursive = (number) => { case number + 1: return list default: - list.push( - sgn ? - list.at(-2) - list.at(-1) - : - list.at(-1) + list.at(-2) - ) + list.push(list.at(-1) + list.at(-2)) return FibonacciRecursive(number) } - })() + })().map((fib, i) => fib * (sgn ? (-1) ** (i + 1) : 1)) } const dict = new Map() From 82861ef86c2efde22bd688a1113220e5a499d142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 16:11:30 -0400 Subject: [PATCH 18/25] Explicitly using negative 0 --- Maths/test/Fibonacci.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index 143e99401f..987dedd103 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -42,7 +42,7 @@ describe('Fibonacci', () => { expect.arrayContaining([0, 1, 1, 2, 3, 5, 8]) ) expect(FibonacciRecursive(-6)).toEqual( - expect.arrayContaining([0, 1, -1, 2, -3, 5, -8]) + expect.arrayContaining([-0, 1, -1, 2, -3, 5, -8]) ) }) From 83afc56314db8c0742466b24ebaeaae59374ce1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 16:16:36 -0400 Subject: [PATCH 19/25] Fixed errors --- Maths/Fibonacci.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index bed7331bbe..41b488c261 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -1,7 +1,7 @@ // https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers const FibonacciIterative = (nth) => { const sign = nth < 0 - if (sign) n = -n + if (sign) nth = -nth const sequence = [0] if (nth >= 1) sequence.push(1) @@ -24,7 +24,7 @@ const FibonacciGenerator = function* (negative) { let b = 1 yield a while (true) { - yield b + yield b; [a, b] = negative ? [b, a - b] : [b, a + b] } } From a034743b238d939e91edac8826e0a4983a50a621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 16:25:57 -0400 Subject: [PATCH 20/25] Fixed part of style --- Maths/Fibonacci.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 41b488c261..f5ff620a2b 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -9,17 +9,16 @@ const FibonacciIterative = (nth) => { for (let i = 2; i < nth; i++) { sequence.push( - sign ? - sequence[i - 1] - sequence[i] - : - sequence[i] + sequence[i - 1] + sign + ? sequence[i - 1] - sequence[i] + : sequence[i] + sequence[i - 1] ) } return sequence } -const FibonacciGenerator = function* (negative) { +const FibonacciGenerator = function * (negative) { let a = 0 let b = 1 yield a @@ -86,10 +85,9 @@ const FibonacciDpWithoutRecursion = (number) => { table.push(sgn ? -1 : 1) for (let i = 2; i < number; ++i) { table.push( - sgn ? - table[i - 1] - table[i] - : - table[i] + table[i - 1] + sgn + ? table[i - 1] - table[i] + : table[i] + table[i - 1] ) } return table From cdbf5ff5c20314b38274da40a089d5e3aacf34fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 21 Jun 2022 16:30:15 -0400 Subject: [PATCH 21/25] Fixed style --- Maths/Fibonacci.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index f5ff620a2b..1810434d2c 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -9,9 +9,7 @@ const FibonacciIterative = (nth) => { for (let i = 2; i < nth; i++) { sequence.push( - sign - ? sequence[i - 1] - sequence[i] - : sequence[i] + sequence[i - 1] + sign ? sequence[i - 1] - sequence[i] : sequence[i] + sequence[i - 1] ) } @@ -85,9 +83,7 @@ const FibonacciDpWithoutRecursion = (number) => { table.push(sgn ? -1 : 1) for (let i = 2; i < number; ++i) { table.push( - sgn - ? table[i - 1] - table[i] - : table[i] + table[i - 1] + sgn ? table[i - 1] - table[i] : table[i] + table[i - 1] ) } return table From 79d4c47b4d0299a495f351b8d8fc5c326195ad60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Wed, 22 Jun 2022 01:36:00 -0400 Subject: [PATCH 22/25] Ran `npx standard --fix Fibonacci.js` From 42d3ae43ec5ed6f8ec5f18ce43c4d4fc65296677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Wed, 22 Jun 2022 01:41:52 -0400 Subject: [PATCH 23/25] Removed trailing space --- Maths/test/Fibonacci.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index 987dedd103..f3dcb98fe7 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -26,7 +26,7 @@ describe('Fibonacci', () => { expect(positive.next().value).toBe(3) expect(positive.next().value).toBe(5) expect(positive.next().value).toBe(8) - + const negative = FibonacciGenerator(true) expect(negative.next().value).toBe(0) expect(negative.next().value).toBe(1) @@ -77,7 +77,7 @@ describe('Fibonacci', () => { expect(FibonacciMatrixExpo(-5)).toBe(5) expect(FibonacciMatrixExpo(-6)).toBe(-8) }) - + it('should return bigint for FibonacciMatrixExpo', () => { expect(FibonacciMatrixExpo(0n)).toBe(0n) expect(FibonacciMatrixExpo(1n)).toBe(1n) From 21ff3fdb25ef70d53af186a38a4222f8639fdbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Sun, 26 Jun 2022 14:01:14 -0400 Subject: [PATCH 24/25] Update Fibonacci.js --- Maths/Fibonacci.js | 59 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 1810434d2c..1186e023e2 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -1,48 +1,48 @@ // https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers -const FibonacciIterative = (nth) => { - const sign = nth < 0 - if (sign) nth = -nth +const FibonacciIterative = (num) => { + const isNeg = num < 0 + if (isNeg) num *= -1 const sequence = [0] - if (nth >= 1) sequence.push(1) - if (nth >= 2) sequence.push(sign ? -1 : 1) + if (num >= 1) sequence.push(1) + if (num >= 2) sequence.push(isNeg ? -1 : 1) - for (let i = 2; i < nth; i++) { + for (let i = 2; i < num; i++) { sequence.push( - sign ? sequence[i - 1] - sequence[i] : sequence[i] + sequence[i - 1] + isNeg ? sequence[i - 1] - sequence[i] : sequence[i] + sequence[i - 1] ) } return sequence } -const FibonacciGenerator = function * (negative) { +const FibonacciGenerator = function * (neg) { let a = 0 let b = 1 yield a while (true) { yield b; - [a, b] = negative ? [b, a - b] : [b, a + b] + [a, b] = neg ? [b, a - b] : [b, a + b] } } const list = [] -const FibonacciRecursive = (number) => { - const sgn = number < 0 - if (sgn) number *= -1 +const FibonacciRecursive = (num) => { + const sgn = num < 0 + if (sgn) num *= -1 return (() => { switch (list.length) { case 0: list.push(0) - return FibonacciRecursive(number) + return FibonacciRecursive(num) case 1: list.push(1) - return FibonacciRecursive(number) - case number + 1: + return FibonacciRecursive(num) + case num + 1: return list default: list.push(list.at(-1) + list.at(-2)) - return FibonacciRecursive(number) + return FibonacciRecursive(num) } })().map((fib, i) => fib * (sgn ? (-1) ** (i + 1) : 1)) } @@ -75,13 +75,13 @@ const FibonacciRecursiveDP = (stairs) => { // a function of the number of input bits // @Satzyakiz -const FibonacciDpWithoutRecursion = (number) => { - const sgn = number < 0 - if (sgn) number *= -1 +const FibonacciDpWithoutRecursion = (num) => { + const sgn = num < 0 + if (sgn) num *= -1 const table = [0] table.push(1) table.push(sgn ? -1 : 1) - for (let i = 2; i < number; ++i) { + for (let i = 2; i < num; ++i) { table.push( sgn ? table[i - 1] - table[i] : table[i] + table[i - 1] ) @@ -152,7 +152,10 @@ const matrixExpo = (A, n) => { return result } -const FibonacciMatrixExpo = (n) => { +const FibonacciMatrixExpo = (num) => { + const isBigInt = typeof num === 'bigint' + const ZERO = isBigInt ? 0n : 0 + const ONE = isBigInt ? 1n : 1 // F(0) = 0, F(1) = 1 // F(n) = F(n-1) + F(n-2) // Consider below matrix multiplication: @@ -165,27 +168,23 @@ const FibonacciMatrixExpo = (n) => { // or F(n, n-1) = A * A * F(n-2, n-3) // or F(n, n-1) = pow(A, n-1) * F(1, 0) - if (n === 0 || n === 0n) return n - - const sgn = n < 0 - if (sgn) n = -n + if (num === ZERO) return num - const isBigInt = typeof n === 'bigint' - const ZERO = isBigInt ? 0n : 0 - const ONE = isBigInt ? 1n : 1 + const sgn = num < 0 + if (sgn) num *= -ONE const A = [ [ONE, ONE], [ONE, ZERO] ] - const poweredA = matrixExpo(A, n - ONE) // A raised to the power n-1 + const poweredA = matrixExpo(A, num - ONE) // A raised to the power n-1 let F = [ [ONE], [ZERO] ] F = matrixMultiply(poweredA, F) - return F[0][0] * (sgn ? (-ONE) ** (n + ONE) : ONE) + return F[0][0] * (sgn ? (-ONE) ** (num + ONE) : ONE) } export { FibonacciDpWithoutRecursion } From 81e64a431a45918a9e980896276d9df0d3f0446c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Sun, 26 Jun 2022 14:06:22 -0400 Subject: [PATCH 25/25] Update Fibonacci.js --- Maths/Fibonacci.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 1186e023e2..fc8102ad2c 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -28,8 +28,8 @@ const FibonacciGenerator = function * (neg) { const list = [] const FibonacciRecursive = (num) => { - const sgn = num < 0 - if (sgn) num *= -1 + const isNeg = num < 0 + if (isNeg) num *= -1 return (() => { switch (list.length) { case 0: @@ -44,24 +44,24 @@ const FibonacciRecursive = (num) => { list.push(list.at(-1) + list.at(-2)) return FibonacciRecursive(num) } - })().map((fib, i) => fib * (sgn ? (-1) ** (i + 1) : 1)) + })().map((fib, i) => fib * (isNeg ? (-1) ** (i + 1) : 1)) } const dict = new Map() const FibonacciRecursiveDP = (stairs) => { - const sgn = stairs < 0 - if (sgn) stairs *= -1 + const isNeg = stairs < 0 + if (isNeg) stairs *= -1 if (stairs <= 1) return stairs // Memoize stair count - if (dict.has(stairs)) return (sgn ? (-1) ** (stairs + 1) : 1) * dict.get(stairs) + if (dict.has(stairs)) return (isNeg ? (-1) ** (stairs + 1) : 1) * dict.get(stairs) const res = FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2) dict.set(stairs, res) - return (sgn ? (-1) ** (stairs + 1) : 1) * res + return (isNeg ? (-1) ** (stairs + 1) : 1) * res } // Algorithms @@ -76,14 +76,14 @@ const FibonacciRecursiveDP = (stairs) => { // @Satzyakiz const FibonacciDpWithoutRecursion = (num) => { - const sgn = num < 0 - if (sgn) num *= -1 + const isNeg = num < 0 + if (isNeg) num *= -1 const table = [0] table.push(1) - table.push(sgn ? -1 : 1) + table.push(isNeg ? -1 : 1) for (let i = 2; i < num; ++i) { table.push( - sgn ? table[i - 1] - table[i] : table[i] + table[i - 1] + isNeg ? table[i - 1] - table[i] : table[i] + table[i - 1] ) } return table @@ -170,8 +170,8 @@ const FibonacciMatrixExpo = (num) => { if (num === ZERO) return num - const sgn = num < 0 - if (sgn) num *= -ONE + const isNeg = num < 0 + if (isNeg) num *= -ONE const A = [ [ONE, ONE], @@ -184,7 +184,7 @@ const FibonacciMatrixExpo = (num) => { [ZERO] ] F = matrixMultiply(poweredA, F) - return F[0][0] * (sgn ? (-ONE) ** (num + ONE) : ONE) + return F[0][0] * (isNeg ? (-ONE) ** (num + ONE) : ONE) } export { FibonacciDpWithoutRecursion }