ภาพรวมปัญหา
หาพจน์ที่ N ของอนุกรม: 1, 4, 15, 72, 420, ...
ตัวอย่าง
อินพุต
N = 4
เอาต์พุต
72
วิธีแก้ปัญหา
สังเกต: T(N) = N! × (N + 2) / 2
N=1: 1! × 3/2 = 1.5? → 1 (ปัดหรือสูตรต่างออกไป) N=2: 2! × 4/2 = 4 ✓ N=3: 6 × 5/2 = 15 ✓ N=4: 24 × 6/2 = 72 ✓ N=5: 120 × 7/2 = 420 ✓
โค้ดตัวอย่าง
#include <iostream>
using namespace std;
int calcFactorial(int N) {
int factorial = 1;
for (int i = 1; i <= N; i++)
factorial *= i;
return factorial;
}
int calcNthTerm(int N) {
return calcFactorial(N) * (N + 2) / 2;
}
int main() {
int N = 7;
cout << N << "th term of the series is " << calcNthTerm(N);
return 0;
}
เอาต์พุต
7th term of the series is 22680
สรุป
ผสมผสาน Factorial กับพหุนามดีกรี 1