-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFactorialOfLargeNumbers.js
43 lines (41 loc) · 1.17 KB
/
FactorialOfLargeNumbers.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
/**
* @author Anirudh Sharma
*
* Given an integer, the task is to find factorial of the number.
*/
const findFactorial = (n) => {
// The array to store the digits of factorial
const factorial = new Array(2568);
// Initialize the result
factorial[0] = 1;
// Number of digits
let digits = 1;
// Loop for 2 to n
for (let i = 2; i <= n; i++) {
digits = multiply(i, factorial, digits);
}
return factorial.slice(0, digits).reverse().join("");
};
const multiply = (value, factorial, digits) => {
// Carry
let carry = 0;
// Multiply i with every element of factorial
for (let i = 0; i < digits; i++) {
// Product with current element
let product = factorial[i] * value + carry;
factorial[i] = product % 10;
carry = parseInt(product / 10);
}
// Put carry in factorial and increase digits
while (carry != 0) {
factorial[digits] = carry % 10;
carry = parseInt(carry / 10);
digits++;
}
return digits;
};
console.log(findFactorial(10));
console.log(findFactorial(50));
console.log(findFactorial(100));
console.log(findFactorial(500));
console.log(findFactorial(1000));