Trong bài toán này, cho năm giá trị m, n, mth term, nth term, p. Nhiệm vụ là tìm số hạng P của cấp số nhân (GP) nếu biết số hạng M và N.
Cho GP, biết giá trị số hạng m và n. Dùng các giá trị này tìm số hạng p của dãy.
Ví dụ minh họa:
Đầu vào
m = 7, mthTerm = 1458, n = 10, nthterm = 39366, p = 3
Đầu ra
18
Phương pháp giải
Giả sử GP: a, a*r, a*r², a*r³... Công thức số hạng T: a * r^(T-1).
mth term = a * r^(m-1), nth term = a * r^(n-1). Chia 2 vế: mth/nth = r^(m-n). Tìm r, sau đó tìm a từ mth term = a * r^(m-1). Cuối cùng pth term = a * r^(p-1).
Chương trình minh họa:
Ví dụ
#include <cmath>
#include <iostream>
using namespace std;
double findRinGP(double m, double n, double mth, double nth) {
if (m < n) return pow(nth / mth, 1.0 / (n - m));
return pow(mth / nth, 1.0 / (m - n));
}
double findTermGP(int m, int n, double mth, double nth, int p) {
double r = findRinGP(m, n, mth, nth);
double a = mth / pow(r, (m - 1));
return (a * pow(r, (p - 1)));
}
int main() {
int m = 7, n = 10, p = 5;
double mth = 1458, nth = 39366;
cout<<"The "<<p<<"th of the series is "<<findTermGP(m, n, mth, nth, p);
return 0;
}Kết quả
The 5th of the series is 162