-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextra-long-factorials.js
55 lines (47 loc) · 1.2 KB
/
extra-long-factorials.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
51
52
53
54
55
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function x(numArr, num) {
let arr = numArr.map(n => n * num);
let point = 0;
while (true) {
let more = Math.floor(arr[point] / 10);
if (more > 0 || point + 1 < arr.length) {
arr[point] = arr[point] % 10;
if (point + 1 < arr.length) {
arr[point + 1] += more;
} else {
arr.push(more);
}
point++;
} else {
break;
}
}
return arr;
}
function extraLongFactorials(n) {
// Complete this function
let result = [1];
for (let i = 1; i <= n; i++) {
result = x(result, i);
}
console.log(result.reverse().join(''));
}
function main() {
var n = parseInt(readLine());
extraLongFactorials(n);
}