-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1429.js
executable file
·44 lines (33 loc) · 859 Bytes
/
1429.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
const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n")
class Factorial {
static #list = new Map([
[0, 1n], [1, 1n]
])
/** @param {number} nth */
static get(nth) {
if (Factorial.#list.has(nth))
return Factorial.#list.get(nth)
let value = Factorial.#list.get(Factorial.#list.size - 1)
for (let f = Factorial.#list.size; f <= nth; f++) {
value *= BigInt(f)
Factorial.#list.set(f, value)
}
return Factorial.#list.get(nth)
}
}
/** @param {number | string} acm*/
function ACMToDec(acm) {
return [...`${acm}`]
.reverse()
.reduce((sum, decimal, index) => sum + Factorial.get(index + 1) * BigInt(decimal), 0n)
}
function main() {
const output = []
for (const line of input) {
if (line == "0") break
else output.push(ACMToDec(line))
}
console.log(output.join("\n"))
}
main()