Skip to content

Commit feaca3b

Browse files
committed
Make Fibonacci.js file comply with standard JS rules
1 parent 78a0597 commit feaca3b

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

Maths/Fibonacci.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ const FibonacciDpWithoutRecursion = (number) => {
7272
// Using Matrix exponentiation to find n-th fibonacci in O(log n) time
7373

7474
const copyMatrix = (A) => {
75-
return A.map(row => row.map(cell => cell));
75+
return A.map(row => row.map(cell => cell))
7676
}
7777

7878
const Identity = (size) => {
79-
const I = Array(size).fill(null).map(() => Array(size).fill());
79+
const I = Array(size).fill(null).map(() => Array(size).fill())
8080
return I.map((row, rowIdx) => row.map((_col, colIdx) => {
8181
return rowIdx === colIdx ? 1 : 0
8282
}))
@@ -90,12 +90,12 @@ const matrixMultiply = (A, B) => {
9090
const l = A.length
9191
const m = B.length
9292
const n = B[0].length // Assuming non-empty matrices
93-
const C = Array(l).fill(null).map(() => Array(n).fill());
94-
for(let i = 0; i < l; i++) {
95-
for(let j = 0; j < n; j++) {
93+
const C = Array(l).fill(null).map(() => Array(n).fill())
94+
for (let i = 0; i < l; i++) {
95+
for (let j = 0; j < n; j++) {
9696
C[i][j] = 0
97-
for(let k = 0; k < m; k++) {
98-
C[i][j] += A[i][k]*B[k][j]
97+
for (let k = 0; k < m; k++) {
98+
C[i][j] += A[i][k] * B[k][j]
9999
}
100100
}
101101
}
@@ -105,41 +105,41 @@ const matrixMultiply = (A, B) => {
105105
// A is a square matrix
106106
const matrixExpo = (A, n) => {
107107
A = copyMatrix(A)
108-
if(n == 0) return Identity(A.length) // Identity matrix
109-
if(n == 1) return A
108+
if (n === 0) return Identity(A.length) // Identity matrix
109+
if (n === 1) return A
110110

111111
// Just like Binary exponentiation mentioned in ./BinaryExponentiationIterative.js
112112
let result = Identity(A.length)
113-
while(n > 0) {
114-
if(n%2 !== 0) result = matrixMultiply(result, A)
115-
n = Math.floor(n/2)
116-
if(n > 0) A = matrixMultiply(A, A)
113+
while (n > 0) {
114+
if (n % 2 !== 0) result = matrixMultiply(result, A)
115+
n = Math.floor(n / 2)
116+
if (n > 0) A = matrixMultiply(A, A)
117117
}
118118
return result
119119
}
120120

121121
const FibonacciMatrixExpo = (n) => {
122122
// F(0) = 0, F(1) = 1
123123
// F(n) = F(n-1) + F(n-2)
124-
// Consider below matrix multiplication:
124+
// Consider below matrix multiplication:
125125

126126
// | F(n) | |1 1| |F(n-1)|
127127
// | | = | | * | |
128128
// |F(n-1)| |1 0| |F(n-2)|
129129

130130
// F(n, n-1) = pow(A, n-1) * F(1, 0)
131131

132-
if(n === 0) return 0;
132+
if (n === 0) return 0
133133

134134
const A = [
135-
[1, 1],
136-
[1, 0]
137-
]
138-
const poweredA = matrixExpo(A, n-1) // A raise to the power n
135+
[1, 1],
136+
[1, 0]
137+
]
138+
const poweredA = matrixExpo(A, n - 1) // A raise to the power n
139139
let F = [
140-
[1],
141-
[0]
142-
]
140+
[1],
141+
[0]
142+
]
143143
F = matrixMultiply(poweredA, F)
144144
return F[0][0]
145145
}

0 commit comments

Comments
 (0)