Giới thiệu
Cho số nguyên N. Tìm số hạng thứ N của dãy: 0, 2, 4, 8, 12, 18…
Ví dụ minh họa
Đầu vào
N = 5
Đầu ra
12
Phân tích dãy
Công thức chung: T(N) = (N + N×(N-1)) / 2 = N²/2 + N/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 = 10;
cout << N << "th term of the series is " << calcNthTerm(N);
return 0;
}
Đầu ra
10th term of the series is 50