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

Chương trình C++ tìm số hạng thứ N của dãy 3, 9, 21, 41, 71…

Giới thiệu

Cho số nguyên N. Tìm số hạng thứ N của dãy: 3, 9, 21, 41, 71…

Ví dụ minh họa

Đầu vào

N = 7

Đầu ra

169

Giải thích

Dãy: 3, 9, 21, 41, 71, 169...

Phân tích dãy

Công thức chung: T(N) = Σi² + Σi + 1 = n(n+1)(2n+1)/6 + n(n+1)/2 + 1

Chương trình minh họa

#include <iostream>
using namespace std;

int findNthTerm(int n) {
    return (n*(n+1)*(2*n+1))/6 + (n*(n+1))/2 + 1;
}

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

Đầu ra

The 12th term of the series is 729