Ratio of mth and nth terms of an A. P. with given ratio of sums

07 Jul, 2022
Given that the ratio to sum of first m and n terms of an A.P. with first term ‘a’ and commond difference ‘d’ is m^2:n^2. The task is to find the ratio of mth and nth term of this A.P. Examples:  
Input: m = 3, n = 2
Output: 1.6667

Input: m = 5, n = 3
Output: 1.8
 
Approach:Let the Sum of first m and n terms be denoted by Sm and Sn respectively. Also, let the mth and nth term be denoted by tm and tn respectively.  
Sm = (m * [ 2*a + (m-1)*d ])/2 Sn = (n * [ 2*a + (n-1)*d ])/2Given: Sm / Sn = m^2 / n^2 Hence, ((m * [ 2*a + (m-1)*d ])/2) / ((n * [ 2*a + (n-1)*d ])/2) = m^2 / n^2 => (2*a + (m-1)*d) / (2*a + (n-1)*d) = m / non cross multiplying and solving, we get d = 2 * a Hence, the mth and nth terms can be written as:mth term = tm = a +(m-1)*d = a + (m-1)*(2*a) nth term = tn = a +(n-1)*d = a + (n-1)*(2*a) Hence the ratio will be: tm / tn = (a + (m-1)*(2*a)) / (a + (n-1)*(2*a)) tm / tn = (2*m – 1) / (2*n – 1)
Below is the required implementation:  
C++
Java
Python3
C#
PHP
Javascript
// C++ code to calculate ratio#include <bits/stdc++.h>using namespace std; // function to calculate ratio of mth and nth termfloat CalculateRatio(float m, float n){    // ratio will be tm/tn = (2*m - 1)/(2*n - 1)    return (2 * m - 1) / (2 * n - 1);} // Driver codeint main(){    float m = 6, n = 2;    cout << CalculateRatio(m, n);     return 0;}
// Java code to calculate ratioimport java.io.*; class Nth {     // function to calculate ratio of mth and nth termstatic float CalculateRatio(float m, float n){    // ratio will be tm/tn = (2*m - 1)/(2*n - 1)    return (2 * m - 1) / (2 * n - 1);}} // Driver codeclass GFG {         public static void main (String[] args) {    float m = 6, n = 2;    Nth a=new Nth();System.out.println(a.CalculateRatio(m, n));     }} // this code is contributed by inder_verma..
# Python3 program to calculate ratio # function to calculate ratio# of mth and nth termdef CalculateRatio(m, n):     # ratio will be tm/tn = (2*m - 1)/(2*n - 1)    return (2 * m - 1) / (2 * n - 1); # Driver codeif __name__=='__main__':    m = 6;    n = 2;    print (float(CalculateRatio(m, n))); # This code is contributed by# Shivi_Aggarwal
// C# code to calculate ratiousing System; class Nth {     // function to calculate ratio of mth and nth termfloat CalculateRatio(float m, float n){    // ratio will be tm/tn = (2*m - 1)/(2*n - 1)    return (2 * m - 1) / (2 * n - 1);}     // Driver code    public static void Main () {    float m = 6, n = 2;    Nth a=new Nth();Console.WriteLine(a.CalculateRatio(m, n));     }}// this code is contributed by anuj_67.
<?php// PHP code to calculate ratio // function to calculate ratio// of mth and nth termfunction CalculateRatio( $m, $n){    // ratio will be tm/tn = (2*m - 1)/(2*n - 1)    return (2 * $m - 1) / (2 * $n - 1);} // Driver code $m = 6; $n = 2;echo CalculateRatio($m, $n); // This code is contributed// by inder_verma?>
<script> // JavaScript code to calculate ratio // function to calculate ratio of mth and nth termfunction CalculateRatio(m, n){    // ratio will be tm/tn = (2*m - 1)/(2*n - 1)    return (2 * m - 1) / (2 * n - 1);} // Driver code     let m = 6, n = 2;    document.write(CalculateRatio(m, n)); // This code is contributed by Surbhi Tyagi. </script>
3.66667
 
Time Complexity: O(1)Auxiliary Space: O(1)
inderDuMCA
vt_m
Shivi_Aggarwal
surbhityagi15
hasani
arithmetic progression
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of subarrays with average K
Count of longest possible subarrays with sum not divisible by K
LCM of given array elements
Algorithm to solve Rubik's Cube
Find whether a given integer is a power of 3 or not
C++ Classes and Objects
Python Dictionary
Object Oriented Programming in C++
Inheritance in C++
Arrays in C/C++