Razón de mth y nth término en una progresión aritmética (AP)

Dados dos valores ‘m’ y ‘n’ y el quinto término de una progresión aritmética es cero. La tarea es encontrar la relación entre el m-ésimo y el n-ésimo término de este PA.
Ejemplos: 
 

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

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

Enfoque: Acc. al enunciado, el quinto término es cero. Ahora entiende el concepto con un ejemplo. Como A5=a+4*d=0. 
Ahora, tenemos que encontrar la razón de m = 10° término y n = 20° término. 
 

A[10] 
= A + 9 * d 
= A5 + 5 * d 
= 0 + 5 * d 
= 5 * d
Del mismo modo, A[20] 
= A + 19 * d 
= A5 + 15 * d 
= 0 + 15 * d 
= 15 * d
Ahora, tenemos que encontrar la razón, entonces Ans= A[10] / A[20] 

A continuación se muestra la implementación requerida: 
 

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
#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

<?php
// PHP implementation of above approach
 
function __gcd($a, $b)
{
    if ($b == 0) return $a;
    return __gcd($b, $a % $b);
}
 
// Function to find the ratio
function 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));
 
    echo $numerator , "/" ,
         $denominator;
}
 
// Driver code
 
// let d=1 as d doesn't affect ratio
$m = 10; $n = 20;
 
findRatio($m, $n);
 
// This code is contributed
// by inder_verma
?>

Javascript

<script>
 
// Javascript implementation of above approach
 
// Function to calculate the GCD
function GCD(a, b) {
    if (b==0) return a;
    return GCD(b,a%b);
    }
 
// Function to find the ratio
function findRatio(m,n)
{
    var Am = m - 5, An = n - 5 ;
     
    // divide numerator by GCD to get
    // smallest fractional value
    var numerator = parseInt(Am / GCD(Am, An)) ;
     
    // divide denominator by GCD to get
    // smallest fractional value
    var denominator = parseInt(An / GCD(Am, An));
     
    document.write(numerator + "/" + denominator);
}
// Driver code
// let d=1 as d doesn't affect ratio
var m = 10, n = 20;
 
findRatio(m, n);
     
</script>
Producción: 

1/3

 

Complejidad de tiempo: O(log(max(m, n))), donde m y n representan los números enteros dados.
Espacio auxiliar: O(1), no se requiere espacio adicional, no, es una constante.

Publicación traducida automáticamente

Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *