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 1, 6, 17, 34, 56... trong C++

Cho dãy: 1, 6, 17, 34, 56, 86, 121, 162... Tìm số hạng thứ N.

Công thức

T_N = 3*N^2 - 4*N + 2.

Mã nguồn C++

#include <iostream>
#include <cmath>
using namespace std;

int nthTerm(int n) {
    return 3 * n * n - 4 * n + 2;
}

int main() {
    int N = 4;
    cout << nthTerm(N) << endl; // 34
    return 0;
}

Đầu ra

34