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 = [ ]
21// https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers
32const FibonacciIterative = ( nth ) => {
43 const sign = nth < 0
@@ -20,7 +19,20 @@ const FibonacciIterative = (nth) => {
2019 return sequence
2120}
2221
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 = [ ]
2333const FibonacciRecursive = ( number ) => {
34+ const sgn = number < 0
35+ if ( sgn ) number *= - 1
2436 return ( ( ) => {
2537 switch ( list . length ) {
2638 case 0 :
@@ -32,7 +44,6 @@ const FibonacciRecursive = (number) => {
3244 case number + 1 :
3345 return list
3446 default :
35- const sgn = number < 0
3647 list . push (
3748 sgn ?
3849 list . at ( - 2 ) - list . at ( - 1 )
@@ -45,7 +56,6 @@ const FibonacciRecursive = (number) => {
4556}
4657
4758const dict = new Map ( )
48-
4959const FibonacciRecursiveDP = ( stairs ) => {
5060 const sgn = stairs < 0
5161 if ( sgn ) stairs *= - 1
@@ -193,6 +203,7 @@ const FibonacciMatrixExpo = (n) => {
193203
194204export { FibonacciDpWithoutRecursion }
195205export { FibonacciIterative }
206+ export { FibonacciGenerator }
196207export { FibonacciRecursive }
197208export { FibonacciRecursiveDP }
198209export { FibonacciMatrixExpo }
You can’t perform that action at this time.
0 commit comments