Giới thiệu
Cho số nguyên N. Tìm số hạng thứ N của dãy: 1, 5, 32, 288…
Ví dụ minh họa
Đầu vào
N = 4
Đầu ra
288
Giải thích
4th term = 4⁴ + 3³ + 2² + 1¹ = 256 + 27 + 4 + 1 = 288
Phân tích dãy
Công thức chung: T(N) = Σ(iⁱ) từ i=1 đến N
Chương trình minh họa
Lưu ý: Code mẫu trong nội dung gốc bị sai (tính giai thừa thay vì lũy thừa). Dưới đây là code đúng:
#include <iostream>
#include <cmath>
using namespace std;
int calcNthTerm(int N) {
long long sum = 0;
for (int i = 1; i <= N; i++) {
sum += pow(i, i);
}
return sum;
}
int main() {
int N = 5;
cout << N << "th term of the series is " << calcNthTerm(N);
return 0;
}
Đầu ra
5th term of the series is 3413