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 3, 12, 29, 54, 87... trong C++

Cho số N. Tìm số hạng thứ N của dãy: 3, 12, 29, 54, 87, 128, ...

Công thức chung

Tn = 4n² - 3n + 2

Ví dụ

T8 = 4×64 - 24 + 2 = 256 - 24 + 2 = 234

Ví dụ minh họa (C++)

#include <iostream>
using namespace std;

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

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

Đầu ra

7th term of the series is 177