Sum of P terms of an AP if Mth and Nth terms are given
Last Updated :
27 May, 2022
Given Mth and Nth terms of arithmetic progression. The task is to find the sum of its first p terms.
Examples:
Input: m = 6, n = 10, mth = 12, nth = 20, p = 5
Output:30
Input:m = 10, n = 20, mth = 70, nth = 140, p = 4
Output:70
Approach: Let a is the first term and d is the common difference of the given AP. Therefore
mth term = a + (m-1)d and
nth term = a + (n-1)d
From these two equations, find the value of a and d. Now use the formula of sum of p terms of an AP.
Sum of p terms =
( p * ( 2*a + (p-1) * d ) ) / 2;
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
pair< double , double > findingValues( double m,
double n, double mth, double nth)
{
double d = ( abs (mth - nth)) / abs ((m - 1) - (n - 1));
double a = mth - ((m - 1) * d);
return make_pair(a, d);
}
double findSum( int m, int n, int mth, int nth, int p)
{
pair< double , double > ad;
ad = findingValues(m, n, mth, nth);
double a = ad.first, d = ad.second;
double sum = (p * (2 * a + (p - 1) * d)) / 2;
return sum;
}
int main()
{
double m = 6, n = 10, mTerm = 12, nTerm = 20, p = 5;
cout << findSum(m, n, mTerm, nTerm, p) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static ArrayList<Integer> findingValues( int m, int n,
int mth, int nth)
{
int d = (Math.abs(mth - nth)) /
Math.abs((m - 1 ) - (n - 1 ));
int a = mth - ((m - 1 ) * d);
ArrayList<Integer> res= new ArrayList<Integer>();
res.add(a);
res.add(d);
return res;
}
static int findSum( int m, int n, int mth,
int nth, int p)
{
ArrayList<Integer> ad = findingValues(m, n, mth, nth);
int a = ad.get( 0 );
int d = ad.get( 1 );
int sum = (p * ( 2 * a + (p - 1 ) * d)) / 2 ;
return sum;
}
public static void main (String[] args)
{
int m = 6 , n = 10 , mTerm = 12 , nTerm = 20 , p = 5 ;
System.out.println(findSum(m, n, mTerm, nTerm, p));
}
}
|
Python3
import math as mt
def findingValues(m, n, mth, nth):
d = (( abs (mth - nth)) /
abs ((m - 1 ) - (n - 1 )))
a = mth - ((m - 1 ) * d)
return a, d
def findSum(m, n, mth, nth, p):
a,d = findingValues(m, n, mth, nth)
Sum = (p * ( 2 * a + (p - 1 ) * d)) / 2
return Sum
m = 6
n = 10
mTerm = 12
nTerm = 20
p = 5
print (findSum(m, n, mTerm, nTerm, p))
|
C#
using System;
using System.Collections;
class GFG
{
static ArrayList findingValues( int m, int n,
int mth, int nth)
{
int d = (Math.Abs(mth - nth)) /
Math.Abs((m - 1) - (n - 1));
int a = mth - ((m - 1) * d);
ArrayList res= new ArrayList();
res.Add(a);
res.Add(d);
return res;
}
static int findSum( int m, int n, int mth,
int nth, int p)
{
ArrayList ad = findingValues(m, n, mth, nth);
int a = ( int )ad[0];
int d = ( int )ad[1];
int sum = (p * (2 * a + (p - 1) * d)) / 2;
return sum;
}
public static void Main ()
{
int m = 6, n = 10, mTerm = 12, nTerm = 20, p = 5;
Console.WriteLine(findSum(m, n, mTerm, nTerm, p));
}
}
|
PHP
<?php
function findingValues( $m , $n , $mth , $nth )
{
$d = ( abs ( $mth - $nth )) /
abs (( $m - 1) - ( $n - 1));
$a = $mth - (( $m - 1) * $d );
return array ( $a , $d );
}
function findSum( $m , $n , $mth , $nth , $p )
{
$ad = findingValues( $m , $n , $mth , $nth );
$a = $ad [0];
$d = $ad [1];
$sum = ( $p * (2 * $a + ( $p - 1) * $d )) / 2;
return $sum ;
}
$m = 6;
$n = 10;
$mTerm = 12;
$nTerm = 20;
$p = 5;
echo findSum( $m , $n , $mTerm , $nTerm , $p );
?>
|
Javascript
<script>
function findingValues(m, n, mth, nth)
{
let d = parseInt(
(Math.abs(mth - nth)) / Math.abs((m - 1) - (n - 1)), 10);
let a = mth - ((m - 1) * d);
let res = [];
res.push(a);
res.push(d);
return res;
}
function findSum(m, n, mth, nth, p)
{
let ad = findingValues(m, n, mth, nth);
let a = ad[0];
let d = ad[1];
let sum = parseInt((p * (2 * a + (p - 1) * d)) / 2, 10);
return sum;
}
let m = 6, n = 10, mTerm = 12, nTerm = 20, p = 5;
document.write(findSum(m, n, mTerm, nTerm, p));
</script>
|
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...