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

หาพจน์ที่ N ของอนุกรม 1, 6, 17, 34, 56, 86, 121, 162,... ใน C++

หาพจน์ที่ N ของอนุกรม: 1, 6, 17, 34, 56, 86, 121, 162, ...

ตัวอย่าง: N = 4 → 34

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

สูตรพจน์ที่ n:

Tn = 3×n² - 4×n + 2

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

#include <iostream>
#include <cmath>
using namespace std;

int nthTerm(int n) {
    return 3 * pow(n, 2) - 4 * n + 2;
}

int main() {
    int N = 4;
    cout << nthTerm(N) << endl;
    return 0;
}

Output

34