11// https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Extension_to_negative_integers
2- const FibonacciIterative = ( nth ) => {
3- const sign = nth < 0
4- if ( sign ) nth = - nth
2+ const FibonacciIterative = ( num ) => {
3+ const isNeg = num < 0
4+ if ( isNeg ) num * = - 1
55 const sequence = [ 0 ]
66
7- if ( nth >= 1 ) sequence . push ( 1 )
8- if ( nth >= 2 ) sequence . push ( sign ? - 1 : 1 )
7+ if ( num >= 1 ) sequence . push ( 1 )
8+ if ( num >= 2 ) sequence . push ( isNeg ? - 1 : 1 )
99
10- for ( let i = 2 ; i < nth ; i ++ ) {
10+ for ( let i = 2 ; i < num ; i ++ ) {
1111 sequence . push (
12- sign ? sequence [ i - 1 ] - sequence [ i ] : sequence [ i ] + sequence [ i - 1 ]
12+ isNeg ? sequence [ i - 1 ] - sequence [ i ] : sequence [ i ] + sequence [ i - 1 ]
1313 )
1414 }
1515
1616 return sequence
1717}
1818
19- const FibonacciGenerator = function * ( negative ) {
19+ const FibonacciGenerator = function * ( neg ) {
2020 let a = 0
2121 let b = 1
2222 yield a
2323 while ( true ) {
2424 yield b ;
25- [ a , b ] = negative ? [ b , a - b ] : [ b , a + b ]
25+ [ a , b ] = neg ? [ b , a - b ] : [ b , a + b ]
2626 }
2727}
2828
2929const list = [ ]
30- const FibonacciRecursive = ( number ) => {
31- const sgn = number < 0
32- if ( sgn ) number *= - 1
30+ const FibonacciRecursive = ( num ) => {
31+ const sgn = num < 0
32+ if ( sgn ) num *= - 1
3333 return ( ( ) => {
3434 switch ( list . length ) {
3535 case 0 :
3636 list . push ( 0 )
37- return FibonacciRecursive ( number )
37+ return FibonacciRecursive ( num )
3838 case 1 :
3939 list . push ( 1 )
40- return FibonacciRecursive ( number )
41- case number + 1 :
40+ return FibonacciRecursive ( num )
41+ case num + 1 :
4242 return list
4343 default :
4444 list . push ( list . at ( - 1 ) + list . at ( - 2 ) )
45- return FibonacciRecursive ( number )
45+ return FibonacciRecursive ( num )
4646 }
4747 } ) ( ) . map ( ( fib , i ) => fib * ( sgn ? ( - 1 ) ** ( i + 1 ) : 1 ) )
4848}
@@ -75,13 +75,13 @@ const FibonacciRecursiveDP = (stairs) => {
7575// a function of the number of input bits
7676// @Satzyakiz
7777
78- const FibonacciDpWithoutRecursion = ( number ) => {
79- const sgn = number < 0
80- if ( sgn ) number *= - 1
78+ const FibonacciDpWithoutRecursion = ( num ) => {
79+ const sgn = num < 0
80+ if ( sgn ) num *= - 1
8181 const table = [ 0 ]
8282 table . push ( 1 )
8383 table . push ( sgn ? - 1 : 1 )
84- for ( let i = 2 ; i < number ; ++ i ) {
84+ for ( let i = 2 ; i < num ; ++ i ) {
8585 table . push (
8686 sgn ? table [ i - 1 ] - table [ i ] : table [ i ] + table [ i - 1 ]
8787 )
@@ -152,7 +152,10 @@ const matrixExpo = (A, n) => {
152152 return result
153153}
154154
155- const FibonacciMatrixExpo = ( n ) => {
155+ const FibonacciMatrixExpo = ( num ) => {
156+ const isBigInt = typeof num === 'bigint'
157+ const ZERO = isBigInt ? 0n : 0
158+ const ONE = isBigInt ? 1n : 1
156159 // F(0) = 0, F(1) = 1
157160 // F(n) = F(n-1) + F(n-2)
158161 // Consider below matrix multiplication:
@@ -165,27 +168,23 @@ const FibonacciMatrixExpo = (n) => {
165168 // or F(n, n-1) = A * A * F(n-2, n-3)
166169 // or F(n, n-1) = pow(A, n-1) * F(1, 0)
167170
168- if ( n === 0 || n === 0n ) return n
169-
170- const sgn = n < 0
171- if ( sgn ) n = - n
171+ if ( num === ZERO ) return num
172172
173- const isBigInt = typeof n === 'bigint'
174- const ZERO = isBigInt ? 0n : 0
175- const ONE = isBigInt ? 1n : 1
173+ const sgn = num < 0
174+ if ( sgn ) num *= - ONE
176175
177176 const A = [
178177 [ ONE , ONE ] ,
179178 [ ONE , ZERO ]
180179 ]
181180
182- const poweredA = matrixExpo ( A , n - ONE ) // A raised to the power n-1
181+ const poweredA = matrixExpo ( A , num - ONE ) // A raised to the power n-1
183182 let F = [
184183 [ ONE ] ,
185184 [ ZERO ]
186185 ]
187186 F = matrixMultiply ( poweredA , F )
188- return F [ 0 ] [ 0 ] * ( sgn ? ( - ONE ) ** ( n + ONE ) : ONE )
187+ return F [ 0 ] [ 0 ] * ( sgn ? ( - ONE ) ** ( num + ONE ) : ONE )
189188}
190189
191190export { FibonacciDpWithoutRecursion }
0 commit comments