Find Pth term of a GP if Mth and Nth terms are given
Last Updated :
28 May, 2022
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 equations,
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++
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
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);
}
r = pow (mth / nth, 1.0 / (m - n));
a = mth / pow (r, (m - 1));
return make_pair(a, r);
}
double FindSum( int m, int n, double mth,
double nth, int p)
{
pair< double , double > ar;
ar = values_of_r_and_a(m, n, mth, nth);
double a = ar.first;
double r = ar.second;
double pth = a * pow (r, (p - 1.0));
return pth;
}
int main()
{
int m = 10, n = 5, p = 15;
double mth = 2560, nth = 80;
cout << FindSum(m, n, mth, nth, p)
<< endl;
return 0;
}
|
Java
import java.util.ArrayList;
class GFG
{
static ArrayList values_of_r_and_a( double m, double n,
double mth, double nth)
{
if (m < n)
{
double t = m;
n = m;
m = t;
t = mth;
mth = nth;
nth = t;
}
double r = Math.pow(mth / nth, 1.0 / (m - n));
double a = mth / Math.pow(r, (m - 1 ));
ArrayList arr = new ArrayList();
arr.add(a);
arr.add(r);
return arr;
}
static double FindSum( double m, double n,
double mth, double nth,
double p)
{
ArrayList ar = values_of_r_and_a(m, n, mth, nth);
double a = ( double )ar.get( 0 );
double r = ( double )ar.get( 1 );
double pth = a * Math.pow(r, (p - 1.0 ));
return pth;
}
public static void main(String[] args)
{
double m = 10 ;
double n = 5 ;
double p = 15 ;
double mth = 2560 ;
double nth = 80 ;
System.out.println(( int )FindSum(m, n, mth, nth, p));
}
}
|
Python3
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
r = pow (mth / / nth, 1.0 / (m - n))
a = mth / / pow (r, (m - 1 ))
return a, r
def FindSum(m, n, mth, nth, p):
a,r = values_of_r_and_a(m, n, mth, nth)
pth = a * pow (r, (p - 1.0 ))
return pth
m, n, p = 10 , 5 , 15
mth, nth = 2560.0 , 80.0
print (FindSum(m, n, mth, nth, p))
|
C#
using System;
using System.Collections;
class GFG
{
static ArrayList values_of_r_and_a( double m, double n,
double mth, double nth)
{
if (m < n)
{
double t = m;
n = m;
m = t;
t = mth;
mth = nth;
nth = t;
}
double r = Math.Pow(mth / nth, 1.0 / (m - n));
double a = mth / Math.Pow(r, (m - 1));
ArrayList arr = new ArrayList();
arr.Add(a);
arr.Add(r);
return arr;
}
static double FindSum( double m, double n,
double mth, double nth,
double p)
{
ArrayList ar = values_of_r_and_a(m, n, mth, nth);
double a = ( double )ar[0];
double r = ( double )ar[1];
double pth = a * Math.Pow(r, (p - 1.0));
return pth;
}
static void Main()
{
double m = 10;
double n = 5;
double p = 15;
double mth = 2560;
double nth = 80;
Console.WriteLine(FindSum(m, n, mth, nth, p));
}
}
|
PHP
<?php
function swap( $a1 , $a2 )
{
$temp = $a1 ;
$a1 = $a2 ;
$a2 = $temp ;
}
function values_of_r_and_a( $m , $n , $mth , $nth )
{
if ( $m < $n )
{
swap( $m , $n );
swap( $mth , $nth );
}
$r = pow( $mth / $nth , 1.0 / ( $m - $n ));
$a = $mth / pow( $r , ( $m - 1));
return array ( $a , $r );
}
function FindSum( $m , $n , $mth , $nth , $p )
{
$ar = values_of_r_and_a( $m , $n , $mth , $nth );
$a = $ar [0];
$r = $ar [1];
$pth = $a * pow( $r , ( $p - 1.0));
return $pth ;
}
$m = 10;
$n = 5;
$p = 15;
$mth = 2560;
$nth = 80;
echo FindSum( $m , $n , $mth , $nth , $p );
?>
|
Javascript
<script>
function values_of_r_and_a(m, n, mth, nth)
{
if (m < n)
{
let t = m;
n = m;
m = t;
t = mth;
mth = nth;
nth = t;
}
let r = Math.pow(mth / nth, 1.0 / (m - n));
let a = mth / Math.pow(r, (m - 1));
let arr = [];
arr.push(a);
arr.push(r);
return arr;
}
function FindSum(m, n, mth, nth, p)
{
let ar = values_of_r_and_a(m, n, mth, nth);
let a = ar[0];
let r = ar[1];
let pth = a * Math.pow(r, (p - 1.0));
return pth;
}
let m = 10;
let n = 5;
let p = 15;
let mth = 2560;
let nth = 80;
document.write(FindSum(m, n, mth, nth, p));
</script>
|
Time Complexity: O(log2m + log2p), where m and p represents the value of the given integers.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...