-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigFactorial.js
50 lines (41 loc) · 1.22 KB
/
BigFactorial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Author: Daniel Okwufulueze
* Date: 09/07/2018
* Name: Factorial
* Purpose: Implement the Factorial function for very large numbers, eg 200!, 500!, 760!, etc.
* Factorial for smaller numbers still works with this implementation..
*/
function multiply (a, b) {
let aArray = a.toString().split("").reverse(); // Bring the lsd to the left
let result = [];
let carry = 0;
for (let j = 0; j < aArray.length; j++) {
let numA = aArray[j];
let multiple = b * numA + carry;
result.push(multiple % 10);
carry = parseInt(multiple / 10, 10);
}
while (carry > 0) {
let resultSize = result.length;
result[resultSize] = carry % 10;
carry = parseInt(carry / 10, 10);
}
return result.reverse().join("");
}
function factorial (n) {
if (n < 0 || isNaN(n)) return -1;
let value = 1;
for (let i = 1; i <= n; i++) {
value = multiply(value, i)
}
return value;
}
// Examples
console.log("100! = ", factorial(100));
console.log("500! = ", factorial(500));
console.log("6! = ", factorial(6));
console.log("3100! = ", factorial(3100));
console.log("2400! = ", factorial(2400));
console.log("10! = ", factorial(10));
console.log("210! = ", factorial(210));
console.log("5000! = ", factorial(5000));