📜  具有给定总和比例的AP的第m个和第n个项的比率

📅  最后修改于: 2021-05-06 07:30:45             🧑  作者: Mango

假设具有第一项’a’和共同差’d’的AP的前m和n项之和的比率为m ^ 2:n ^ 2 。任务是找到此AP的第m项和第n项的比率
例子:

Input: m = 3, n = 2
Output: 1.6667

Input: m = 5, n = 3
Output: 1.8

方法:
令前m和n项的总和分别由Sm和Sn表示。
另外,令第m和第n项分别由tm和tn表示。

以下是所需的实现:

C++
// C++ code to calculate ratio
#include 
using namespace std;
 
// function to calculate ratio of mth and nth term
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 code
int main()
{
    float m = 6, n = 2;
    cout << CalculateRatio(m, n);
 
    return 0;
}


Java
// Java code to calculate ratio
import java.io.*;
 
class Nth {
     
// function to calculate ratio of mth and nth term
static 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 code
class 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
# Python3 program to calculate ratio
 
# function to calculate ratio
# of mth and nth term
def CalculateRatio(m, n):
 
    # ratio will be tm/tn = (2*m - 1)/(2*n - 1)
    return (2 * m - 1) / (2 * n - 1);
 
# Driver code
if __name__=='__main__':
    m = 6;
    n = 2;
    print (float(CalculateRatio(m, n)));
 
# This code is contributed by
# Shivi_Aggarwal


C#
// C# code to calculate ratio
using System;
 
class Nth {
     
// function to calculate ratio of mth and nth term
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 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


Javascript


输出:
3.66667