📜  算术级数(AP)中第m项和第n项的比率

📅  最后修改于: 2021-04-24 20:36:19             🧑  作者: Mango

给定两个值“ m”和“ n”,算术级数的第五项为零。任务是找到此AP的第m项和第n项的比率。
例子:

Input: m = 10, n = 20
Output: 1/3

Input: m = 10, n = 15
Output: 1/2

方法: Acc。声明中,第五项为零。现在通过示例了解概念。由于A5 = a + 4 * d = 0。
现在,我们必须找到m =第10个项与n =第20个项的比率。

以下是所需的实现:

C++
// C++ implementation of above approach
#include 
#define ll long long int
using namespace std;
 
// Function to find the ratio
void findRatio(ll m, ll n)
{
 
    ll Am = m - 5, An = n - 5;
 
    // divide numerator by gcd to get
    // smallest fractional value
    ll numerator = Am / (__gcd(Am, An));
 
    // divide denominator by gcd to get
    // smallest fractional value
    ll denominator = An / (__gcd(Am, An));
 
    cout << numerator << "/" << denominator << endl;
}
 
// Driver code
int main()
{
 
    // let d=1 as d doesn't affect ratio
    ll m = 10, n = 20;
 
    findRatio(m, n);
 
    return 0;
}


Java
// java implementation of above approach
 
public class GFG {
     
    // Function to calculate the GCD
    static int GCD(int a, int b) {
           if (b==0) return a;
           return GCD(b,a%b);
        }
     
    // Function to find the ratio
    static void findRatio(int m,int  n)
    {
        int Am = m - 5, An = n - 5 ;
         
        // divide numerator by GCD to get
        // smallest fractional value
        int numerator = Am / GCD(Am, An) ;
         
        // divide denominator by GCD to get
        // smallest fractional value
        int denominator = An / GCD(Am, An) ;
         
        System.out.println(numerator + "/" + denominator);
    }
    // Driver code
    public static void main (String args[]){
         
        // let d=1 as d doesn't affect ratio 
        int m = 10, n = 20;
           
            findRatio(m, n);
           
    }
 
// This code is contributed by ANKITRAI1
}


Python3
# Python3 implementation of above approach
# Function to find the ratio
 
from fractions import gcd
def findRatio(m,n):
    Am = m - 5
    An = n - 5
     
    # divide numerator by gcd to get
    # smallest fractional value
    numerator=Am//(gcd(Am,An))
 
    # divide denominator by gcd to get
    #smallest fractional value
    denominator = An // (gcd(Am, An))
    print(numerator,'/',denominator)
     
# Driver code
# let d=1 as d doesn't affect ratio
if __name__=='__main__':
    m = 10
    n = 20
    findRatio(m, n)
 
# this code is contributed by sahilshelangia


C#
// C# implementation of above approach
  
using System;
public class GFG {
      
    // Function to calculate the GCD
    static int GCD(int a, int b) {
           if (b==0) return a;
           return GCD(b,a%b);
        }
      
    // Function to find the ratio
    static void findRatio(int m,int  n)
    {
        int Am = m - 5, An = n - 5 ;
          
        // divide numerator by GCD to get
        // smallest fractional value
        int numerator = Am / GCD(Am, An) ;
          
        // divide denominator by GCD to get
        // smallest fractional value
        int denominator = An / GCD(Am, An) ;
          
        Console.Write(numerator + "/" + denominator);
    }
    // Driver code
    public static void Main (){
          
        // let d=1 as d doesn't affect ratio 
        int m = 10, n = 20;
            
            findRatio(m, n);
            
    }
  
 
}


PHP


Javascript


输出:
1/3