ภาพรวมปัญหา
หาผลรวมของอนุกรม 1 + x/1 + x²/2 + x³/3 + ... + xⁿ/n โดยกำหนดค่า x และ n
ตัวอย่าง
Input: x = 2, n = 4 Output: 11.666... Explanation: 1 + 2/1 + 4/2 + 8/3 + 16/4 = 1 + 2 + 2 + 2.666 + 4 = 11.666
ตัวอย่างโค้ด C++
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
double calcSeriesSum(int x, int n) {
double total = 1.0;
for (int i = 1; i <= n; i++)
total += (pow(x, i) / i);
return total;
}
int main() {
int x = 3, n = 6;
cout << fixed << setprecision(5)
<< "Sum = " << calcSeriesSum(x, n);
return 0;
}
ผลลัพธ์
Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^6/6 is 207.85