mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Make Fibonacci.js file comply with standard JS rules
This commit is contained in:
@ -72,11 +72,11 @@ const FibonacciDpWithoutRecursion = (number) => {
|
|||||||
// Using Matrix exponentiation to find n-th fibonacci in O(log n) time
|
// Using Matrix exponentiation to find n-th fibonacci in O(log n) time
|
||||||
|
|
||||||
const copyMatrix = (A) => {
|
const copyMatrix = (A) => {
|
||||||
return A.map(row => row.map(cell => cell));
|
return A.map(row => row.map(cell => cell))
|
||||||
}
|
}
|
||||||
|
|
||||||
const Identity = (size) => {
|
const Identity = (size) => {
|
||||||
const I = Array(size).fill(null).map(() => Array(size).fill());
|
const I = Array(size).fill(null).map(() => Array(size).fill())
|
||||||
return I.map((row, rowIdx) => row.map((_col, colIdx) => {
|
return I.map((row, rowIdx) => row.map((_col, colIdx) => {
|
||||||
return rowIdx === colIdx ? 1 : 0
|
return rowIdx === colIdx ? 1 : 0
|
||||||
}))
|
}))
|
||||||
@ -90,7 +90,7 @@ const matrixMultiply = (A, B) => {
|
|||||||
const l = A.length
|
const l = A.length
|
||||||
const m = B.length
|
const m = B.length
|
||||||
const n = B[0].length // Assuming non-empty matrices
|
const n = B[0].length // Assuming non-empty matrices
|
||||||
const C = Array(l).fill(null).map(() => Array(n).fill());
|
const C = Array(l).fill(null).map(() => Array(n).fill())
|
||||||
for (let i = 0; i < l; i++) {
|
for (let i = 0; i < l; i++) {
|
||||||
for (let j = 0; j < n; j++) {
|
for (let j = 0; j < n; j++) {
|
||||||
C[i][j] = 0
|
C[i][j] = 0
|
||||||
@ -105,8 +105,8 @@ const matrixMultiply = (A, B) => {
|
|||||||
// A is a square matrix
|
// A is a square matrix
|
||||||
const matrixExpo = (A, n) => {
|
const matrixExpo = (A, n) => {
|
||||||
A = copyMatrix(A)
|
A = copyMatrix(A)
|
||||||
if(n == 0) return Identity(A.length) // Identity matrix
|
if (n === 0) return Identity(A.length) // Identity matrix
|
||||||
if(n == 1) return A
|
if (n === 1) return A
|
||||||
|
|
||||||
// Just like Binary exponentiation mentioned in ./BinaryExponentiationIterative.js
|
// Just like Binary exponentiation mentioned in ./BinaryExponentiationIterative.js
|
||||||
let result = Identity(A.length)
|
let result = Identity(A.length)
|
||||||
@ -129,7 +129,7 @@ const FibonacciMatrixExpo = (n) => {
|
|||||||
|
|
||||||
// F(n, n-1) = pow(A, n-1) * F(1, 0)
|
// F(n, n-1) = pow(A, n-1) * F(1, 0)
|
||||||
|
|
||||||
if(n === 0) return 0;
|
if (n === 0) return 0
|
||||||
|
|
||||||
const A = [
|
const A = [
|
||||||
[1, 1],
|
[1, 1],
|
||||||
|
Reference in New Issue
Block a user