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 7, 21, 49, 91, 147... trong C++

Cho số n. Tìm số hạng thứ n của dãy: 7, 21, 49, 91, 147, 217, ...

Phân tích dãy

Dãy có_factor 7: 7 × (1, 3, 7, 13, 21, 31, ...)

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

Công thức chung

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

Ví dụ

T4 = 7 × (16 - 3) = 91

T7 = 7 × (49 - 6) = 301

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

#include <iostream>
using namespace std;

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

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

Đầu ra

9th term of the series is 511