S(n) = 1/1! + 1/2! + ... + 1/n!
function factorial(n) {
if (n <= 1) {
return 1;
} else {
return (n * factorial(n - 1));
}
}
let n = 3;
let sum = 0;
for (let i = n; i >= 1; i--) {
sum = sum + (1 / factorial(i));
}
/* 1/1! + 1/2! + 1/3! = 1.6 */
console.log(sum);
function factorial(n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
};
};
function S(n) {
if (n < 1) {
return 0;
} else {
return (1 / factorial(n)) + S(n - 1);
};
}
function factorial(n) {
return n > 1 ? (n * factorial(n - 1)) : 1;
}
function S(n) {
return n > 0 ? ((1 / factorial(n)) + S(n - 1)) : 0;
}
const sumFactorials = (maxN) => {
let factorial = 1;
const getNextFactorial = (n) => {
factorial *= n;
return factorial;
}
const adder = (sum, n) =>
n < maxN ?
1 / getNextFactorial(n) + adder(sum, n + 1) :
sum + 1 / getNextFactorial(n);
return adder(0, 1);
}
return sumFactorials(3);