ภาพรวมปัญหา
หาพจน์ที่ N ของอนุกรม: 2, 10, 30, 68, 130, ...
ตัวอย่าง
อินพุต
N = 7
เอาต์พุต
350
วิธีแก้ปัญหา
สังเกต: T(N) = N³ + N
N=1: 1+1=2 N=2: 8+2=10 N=3: 27+3=30 N=4: 64+4=68 N=5: 125+5=130
โค้ดตัวอย่าง
#include <iostream>
using namespace std;
int findNthTerm(int N) {
return (N * N * N) + N;
}
int main() {
int N = 8;
cout << "The " << N << "th term of the series is " << findNthTerm(N);
return 0;
}
เอาต์พุต
The 8th term of the series is 520
สรุป
สูตร N³ + N เรียบง่าย คำนวณ O(1)