1
1
// 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
5
5
const sequence = [ 0 ]
6
6
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 )
9
9
10
- for ( let i = 2 ; i < nth ; i ++ ) {
10
+ for ( let i = 2 ; i < num ; i ++ ) {
11
11
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 ]
13
13
)
14
14
}
15
15
16
16
return sequence
17
17
}
18
18
19
- const FibonacciGenerator = function * ( negative ) {
19
+ const FibonacciGenerator = function * ( neg ) {
20
20
let a = 0
21
21
let b = 1
22
22
yield a
23
23
while ( true ) {
24
24
yield b ;
25
- [ a , b ] = negative ? [ b , a - b ] : [ b , a + b ]
25
+ [ a , b ] = neg ? [ b , a - b ] : [ b , a + b ]
26
26
}
27
27
}
28
28
29
29
const 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
33
33
return ( ( ) => {
34
34
switch ( list . length ) {
35
35
case 0 :
36
36
list . push ( 0 )
37
- return FibonacciRecursive ( number )
37
+ return FibonacciRecursive ( num )
38
38
case 1 :
39
39
list . push ( 1 )
40
- return FibonacciRecursive ( number )
41
- case number + 1 :
40
+ return FibonacciRecursive ( num )
41
+ case num + 1 :
42
42
return list
43
43
default :
44
44
list . push ( list . at ( - 1 ) + list . at ( - 2 ) )
45
- return FibonacciRecursive ( number )
45
+ return FibonacciRecursive ( num )
46
46
}
47
47
} ) ( ) . map ( ( fib , i ) => fib * ( sgn ? ( - 1 ) ** ( i + 1 ) : 1 ) )
48
48
}
@@ -75,13 +75,13 @@ const FibonacciRecursiveDP = (stairs) => {
75
75
// a function of the number of input bits
76
76
// @Satzyakiz
77
77
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
81
81
const table = [ 0 ]
82
82
table . push ( 1 )
83
83
table . push ( sgn ? - 1 : 1 )
84
- for ( let i = 2 ; i < number ; ++ i ) {
84
+ for ( let i = 2 ; i < num ; ++ i ) {
85
85
table . push (
86
86
sgn ? table [ i - 1 ] - table [ i ] : table [ i ] + table [ i - 1 ]
87
87
)
@@ -152,7 +152,10 @@ const matrixExpo = (A, n) => {
152
152
return result
153
153
}
154
154
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
156
159
// F(0) = 0, F(1) = 1
157
160
// F(n) = F(n-1) + F(n-2)
158
161
// Consider below matrix multiplication:
@@ -165,27 +168,23 @@ const FibonacciMatrixExpo = (n) => {
165
168
// or F(n, n-1) = A * A * F(n-2, n-3)
166
169
// or F(n, n-1) = pow(A, n-1) * F(1, 0)
167
170
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
172
172
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
176
175
177
176
const A = [
178
177
[ ONE , ONE ] ,
179
178
[ ONE , ZERO ]
180
179
]
181
180
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
183
182
let F = [
184
183
[ ONE ] ,
185
184
[ ZERO ]
186
185
]
187
186
F = matrixMultiply ( poweredA , F )
188
- return F [ 0 ] [ 0 ] * ( sgn ? ( - ONE ) ** ( n + ONE ) : ONE )
187
+ return F [ 0 ] [ 0 ] * ( sgn ? ( - ONE ) ** ( num + ONE ) : ONE )
189
188
}
190
189
191
190
export { FibonacciDpWithoutRecursion }
0 commit comments