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 4, 14, 28, 46, 68... trong C++

Cho số N. Tìm số hạng thứ N của dãy: 4, 14, 28, 46, 68, 94, 124, 158, ...

Phân tích dãy

Dãy có factor 2: 2 × (2, 7, 14, 23, 34, ...)

Dãy trong ngoặc: 1²+1, 2²+3, 3²+5, 4²+7, 5²+9, ... = n² + (2n-1)

Công thức chung

Tn = 2 × (n² + 2n - 1)

Ví dụ

T6 = 2 × (36 + 12 - 1) = 2 × 47 = 94

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

#include <iostream>
using namespace std;

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

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

Đầu ra

11th term of the series is 284