File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change 1
- const list = [ ]
2
1
// https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers
3
2
const FibonacciIterative = ( nth ) => {
4
3
const sign = nth < 0
@@ -20,7 +19,20 @@ const FibonacciIterative = (nth) => {
20
19
return sequence
21
20
}
22
21
22
+ const FibonacciGenerator = function * ( sign ) {
23
+ let a = 0
24
+ let b = 1
25
+ yield a
26
+ while ( true ) {
27
+ yield b
28
+ [ a , b ] = sign ? [ b , a - b ] : [ b , a + b ]
29
+ }
30
+ }
31
+
32
+ const list = [ ]
23
33
const FibonacciRecursive = ( number ) => {
34
+ const sgn = number < 0
35
+ if ( sgn ) number *= - 1
24
36
return ( ( ) => {
25
37
switch ( list . length ) {
26
38
case 0 :
@@ -32,7 +44,6 @@ const FibonacciRecursive = (number) => {
32
44
case number + 1 :
33
45
return list
34
46
default :
35
- const sgn = number < 0
36
47
list . push (
37
48
sgn ?
38
49
list . at ( - 2 ) - list . at ( - 1 )
@@ -45,7 +56,6 @@ const FibonacciRecursive = (number) => {
45
56
}
46
57
47
58
const dict = new Map ( )
48
-
49
59
const FibonacciRecursiveDP = ( stairs ) => {
50
60
const sgn = stairs < 0
51
61
if ( sgn ) stairs *= - 1
@@ -193,6 +203,7 @@ const FibonacciMatrixExpo = (n) => {
193
203
194
204
export { FibonacciDpWithoutRecursion }
195
205
export { FibonacciIterative }
206
+ export { FibonacciGenerator }
196
207
export { FibonacciRecursive }
197
208
export { FibonacciRecursiveDP }
198
209
export { FibonacciMatrixExpo }
You can’t perform that action at this time.
0 commit comments