Skip to content

Commit 69bbe51

Browse files
authored
Fixed FRDP yet again
1 parent 028ebe9 commit 69bbe51

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Maths/Fibonacci.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ const FibonacciIterative = (nth) => {
1919
return sequence
2020
}
2121

22-
const FibonacciGenerator = function* (sign) {
22+
const FibonacciGenerator = function* (negative) {
2323
let a = 0
2424
let b = 1
2525
yield a
2626
while (true) {
2727
yield b
28-
[a, b] = sign ? [b, a - b] : [b, a + b]
28+
[a, b] = negative ? [b, a - b] : [b, a + b]
2929
}
3030
}
3131

@@ -63,15 +63,13 @@ const FibonacciRecursiveDP = (stairs) => {
6363
if (stairs <= 1) return stairs
6464

6565
// Memoize stair count
66-
if (dict.has(stairs)) return dict.get(stairs)
66+
if (dict.has(stairs)) return (sgn ? (-1) ** (stairs + 1) : 1) * dict.get(stairs)
6767

68-
const res = sgn
69-
? FibonacciRecursiveDP(stairs - 2) - FibonacciRecursiveDP(stairs - 1)
70-
: FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2)
68+
const res = FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2)
7169

7270
dict.set(stairs, res)
7371

74-
return res
72+
return (sgn ? (-1) ** (stairs + 1) : 1) * res
7573
}
7674

7775
// Algorithms

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy