Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

หาพจน์ที่ N ของอนุกรม 2, 12, 28, 50, 77, 112, 152, 198,... ใน C++

หาพจน์ที่ N ของอนุกรม: 2, 12, 28, 50, 77, 112, 152, 198, ...

ตัวอย่าง: N = 6 → 112

วิธีแก้ปัญหา (Quadratic Formula)

อนุกรมเพิ่มขึ้นแบบพาราโบลิก สูตรทั่วไปเป็นสมการกำลังสอง:

TN = 3×N² + N - 2

ตัวอย่างโค้ด C++

#include <iostream>
using namespace std;

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

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

Output

10th term of the series is 308