Tính tổng S = 1 + Σ_{i=1}^n x^i / (i+1)!.
Ví dụ
x = 6, n = 4: 1 + 6/2 + 36/6 + 216/24 + 1296/120 = 1 + 3 + 6 + 9 + 10.8 = 29.8.
Tối ưu tính toán
Quan hệ truy hồi (bắt đầu từ term = 1):
term_1 = x/2
term_i = term_{i-1} * x / (i+1)
Cài đặt C++
#include <iostream>
using namespace std;
float calcSeriesSum(int x, int n) {
float sum = 1, term = 1;
for (float i = 2; i <= n + 1; i++) {
term *= x / i;
sum += term;
}
return sum;
}
int main() {
int x = 6, n = 4;
cout << "Tong chuoi la " << calcSeriesSum(x, n);
return 0;
}
Output
Tong chuoi la 29.8
Lưu ý
- Dùng
doublecho chính xác hơn. - Cấu trúc tương tự chuỗi e^x nhưng dịch chỉ số mẫu số.