
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find pth Term of a GP Given mth and nth Terms in C++
In this problem we are given five values m, n, mth term, nth term, p. Our task is to Find Pth term of a GP if Mth and Nth terms are given.
For a GP, we are given the values of mth term and nth term. Using these values, we need to find the Pth term of the series.
Let’s take an example to understand the problem,
Input
m = 7, mthTerm = 1458, n = 10, nthterm = 39366, p = 3
Output
18
Solution Approach
Here, we are given a GP. let's suppose the GP is,
GP = a , a*r , a*(r2), a*(r3) ….
The formula for Tth term is
Tth Term = a * r(T-1)
Now, we are given nth and mth term,
mth term = a * (r ^ (m-1)) nth term = a * (r ^ (n-1))
Dividing the equations we get,
mth term / nth term = (r ^(m - n))
Using this equation, we can find the value of r, Then a can be found using the value of mth term as
mth term = a * (r^(m-1))
Then after finding the value of a and r. The value of pth term can be easily found using,
pth term = a * (r^(p-1))
Program to illustrate the working of our solution,
Example
#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; }
Output
The 5th of the series is 162
Advertisements