Sum of P terms of an AP if Mth and Nth terms are given | Function to calculate the value of the ; Calculate value of d using formula ; Calculate value of a using formula ; Return pair ; Function to calculate value sum of first p numbers of the series ; First calculate value of a and d ; Calculate the sum by using formula ; Return the sum ; Driver Code
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 ) ) ;