ในปัญหานี้ เราได้รับจำนวนเต็ม N งานของเราคือการหาพจน์ที่ N ของอนุกรม −
9, 45, 243, 1377, 8019, …
ตัวอย่างเพื่อความเข้าใจ
Input : N = 4 Output : 1377
แนวทางแก้ปัญหา
วิธีแก้ที่ง่ายคือสังเกตรูปแบบของอนุกรม จากการสังเกต สามารถเขียนสูตรได้ดังนี้:
(1¹ + 2¹)×3¹ + (1² + 2²)×3² + (1³ + 2³)×3³ … + (1ⁿ + 2ⁿ)×3ⁿ
ตัวอย่างโค้ด
#include <iostream>
#include <math.h>
using namespace std;
long findNthTermSeries(int n) {
return ((pow(1, n) + pow(2, n)) * pow(3, n));
}
int main() {
int n = 4;
cout << n << "th term of the series is " << findNthTermSeries(n);
return 0;
}
ผลลัพธ์
4th term of the series is 1377