Find Pth term of a GP if Mth and Nth terms are given

Given Mth and Nth term of a Geometric progression. Find its Pth term.

Examples:

Input: m = 10, n = 5, mth = 2560, nth = 80, p = 30
Output: pth = 81920

Input: m = 8, n = 2, mth = 1250, nth = 960, p = 15
Output: 24964.4

Approach:



Let a is the first term and r is the common ratio of the given Geometric Progression. Therefore

mth term = a * pow ( r, (m-1) ) ....... (i) and
nth term = a * pow ( r, (n-1) ) ....... (ii)

For convenience, it is assumed that m > n
From these 2 equation,
Since we have given values m, n, mth term, and nth term, therefore

r = pow(A/B, 1.0/(m-n))

and
Now put the value of r in any of above two equation and calculate the value of a.

a = mth term / pow ( r, (m-1) ) or
a = nth term / pow ( r, (n-1) )

After finding the value of a and r, use the formula of Pth terms of a GP.

pth term of GP = a * pow ( r, (p-1.0) );

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
  
// function to calculate the value
// of the a and r of geometric series
pair<double, double> values_of_r_and_a(double m,
                                       double n,
                                       double mth,
                                       double nth)
{
    double a, r;
  
    if (m < n) {
        swap(m, n);
        swap(mth, nth);
    }
  
    // calculate value of r using formula
    r = pow(mth / nth, 1.0 / (m - n));
  
    // calculate value of a using value of r
    a = mth / pow(r, (m - 1));
  
    // push both values in the vector and return it
    return make_pair(a, r);
}
  
// function to calculate the value
// of pth term of the series
double FindSum(int m, int n, double mth,
               double nth, int p)
{
    pair<double, double> ar;
  
    // first calculate value of a and r
    ar = values_of_r_and_a(m, n, mth, nth);
  
    double a = ar.first;
    double r = ar.second;
  
    // calculate pth term by using formula
    double pth = a * pow(r, (p - 1.0));
  
    // return the value of pth term
    return pth;
}
  
// Driven program to test
int main()
{
    int m = 10, n = 5, p = 15;
    double mth = 2560, nth = 80;
    cout << FindSum(m, n, mth, nth, p)
         << endl;
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for above approach
  
# function to calculate the value
# of the a and r of geometric series
def values_of_r_and_a(m, n, mth, nth):
  
    a, r = 0.0, 0.0
  
    if (m < n):
        m, n = n, m
        mth, nth = mth, nth
  
    # calculate value of r using formula
    r = pow(mth // nth, 1.0 /(m - n))
  
    # calculate value of a using value of r
    a = mth // pow(r, (m - 1))
  
    # push both values in the vector
    # and return it
    return a, r
  
# function to calculate the value
# of pth term of the series
def FindSum(m, n, mth, nth, p):
  
  
    # first calculate value of a and r
    a,r = values_of_r_and_a(m, n, mth, nth)
  
    # calculate pth term by using formula
    pth = a * pow(r, (p - 1.0))
  
    # return the value of pth term
    return pth
  
# Driven Code
m, n, p = 10, 5, 15
mth, nth = 2560.0, 80.0
print(FindSum(m, n, mth, nth, p))
      
# This code is contributed by 
# Mohit kumar 29

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Php implementation of the above approach
function swap($a1, $a2)
{
    $temp = $a1;
    $a1 = $a2;
    $a2 = $temp;
}
  
// function to calculate the value 
// of the a and r of geometric series 
function values_of_r_and_a($m, $n, $mth, $nth
    if ($m < $n)
    
        swap($m, $n); 
        swap($mth, $nth); 
    
  
    // calculate value of r using formula 
    $r = pow($mth / $nth, 1.0 / ($m - $n)); 
  
    // calculate value of a using value of r 
    $a = $mth / pow($r, ($m - 1)); 
  
    // push both values in the vector 
    // and return it 
    return array($a, $r); 
  
// function to calculate the value 
// of pth term of the series 
function FindSum($m, $n, $mth, $nth, $p
  
    // first calculate value of a and r 
    $ar = values_of_r_and_a($m, $n, $mth, $nth); 
  
    $a = $ar[0]; 
    $r = $ar[1]; 
  
    // calculate pth term by using formula 
    $pth = $a * pow($r, ($p - 1.0)); 
  
    // return the value of pth term 
    return $pth
  
// Driver Code
$m = 10;
$n = 5;
$p = 15; 
  
$mth = 2560;
$nth = 80;
  
echo FindSum($m, $n, $mth, $nth, $p);
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

81920


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, Ryuga