Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem 8. Calculate 1 + 1!/X + 2!/X^2 + … + N!/X^N
- //
- // Write a program that, for a given two integer numbers n and x, calculates the sum S = 1 + 1!/x + 2!/x2 + … + n!/x^n.
- //
- // Use only one loop. Print the result with 5 digits after the decimal point.
- // Note that each element can be calculated from the previous by this "formula": (previous_element) * i / x
- //
- // Examples:
- // n x S
- // 3 2 2.75000
- // 4 3 2.07407
- // 5 -4 0.75781
- let input = ['3', '2'];
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- let n = Number(gets());
- let x = Number(gets());
- let s = 1;
- let result = 1;
- for (let i = 1; i <= n; i++){
- result = result * i;
- s = s + result / Math.pow(x, i);
- }
- console.log(s.toFixed(5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement