Giới thiệu
Cho số nguyên N. Tìm số hạng thứ N của dãy: 5, 13, 25, 41, 61, …
Ví dụ minh họa
Đầu vào
N = 5
Đầu ra
61
Phân tích dãy
Quan sát: 5 = 1²+2², 13 = 2²+3², 25 = 3²+4², 41 = 4²+5², 61 = 5²+6²
Công thức chung: T(N) = N² + (N+1)²
Chương trình minh họa
#include <iostream>
using namespace std;
int calcNthTerm(int N) {
return (N * N) + ((N + 1) * (N + 1));
}
int main() {
int N = 7;
cout << N << "th term of the series is " << calcNthTerm(N);
return 0;
}
Đầu ra
7th term of the series is 113