Giới thiệu
Cho số nguyên N. Tìm số hạng thứ N của dãy: 1, 6, 18, 40, 75…
Ví dụ minh họa
Đầu vào
N = 4
Đầu ra
40
Giải thích
4th term = (4 * 4 * 5) / 2 = 40
Phân tích dãy
Công thức chung: T(N) = N² × (N + 1) / 2
Chương trình minh họa
#include <iostream>
using namespace std;
int calcNthTerm(int N) {
return (N * N * (N + 1)) / 2;
}
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 75