Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Tìm số hạng thứ N của dãy 2, 12, 28, 50, 77... trong C++

Cho dãy số: 2, 12, 28, 50, 77, 112, 152, 198,... Tìm số hạng thứ N.

Phân tích

Dãy tăng theo quy luật parabolic → công thức bậc 2: T_N = 3*N^2 + N - 2.

Mã nguồn C++

#include <iostream>
using namespace std;

int findNTerm(int N) {
    return 3*N*N + N - 2;
}

int main() {
    int N = 10;
    cout << N << "th term of the series is " << findNTerm(N);
    return 0;
}

Đầu ra

10th term of the series is 308