- Consider the following numbers (where
n!
isfactorial(n)
):
u1 = (1 / 1!) * (1!)
u2 = (1 / 2!) * (1! + 2!)
u3 = (1 / 3!) * (1! + 2! + 3!)
...
un = (1 / n!) * (1! + 2! + 3! + ... + n!)
-
Which will win:
1 / n!
or(1! + 2! + 3! + ... + n!)
? -
Are these numbers going to
0
because of1/n!
or to infinity due to the sum of factorials or to another number?
-
Calculate
(1 / n!) * (1! + 2! + 3! + ... + n!)
for a given n, where n is an integer greater or equal to 1. -
To avoid discussions about rounding, return the result truncated to 6 decimal places, for example:
1.0000989217538616 will be truncated to 1.000098
1.2125000000000001 will be truncated to 1.2125
- Keep in mind that factorials grow rather rapidly, and you need to handle large inputs.
- You could try to simplify the expression.
function going(n) {
let res = 1,
acc = 1;
for (let i = n; i > 1; i--) {
acc /= i;
res += acc;
}
return Number(String(res).slice(0, 8));
}
console.log(going(5)); // 1.275
console.log(going(6)); // 1.2125
console.log(going(7)); // 1.173214