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

หาพจน์ที่ N ของอนุกรม 0, 9, 22, 39, 60, 85, 114, 147,... ใน C++

หาพจน์ที่ N ของอนุกรม: 0, 9, 22, 39, 60, 85, 114, 147, ...

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

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

การเติบโตแบบพาราโบลิก สูตรทั่วไป:

TN = 2×N² + 3×N - 5

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

#include <iostream>
using namespace std;

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

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

Output

8th term of the series is 147