Given two values ‘m’ and ‘n’ and the 5th term of an arithmetic progression is zero. The task is to find the ratio of mth and nth term of this AP.
Examples:
Input: m = 10, n = 20
Output: 1/3
Input: m = 10, n = 15
Output: 1/2
Approach: Acc. to the statement, 5th term is zero. Now understand the concept with an example. As A5=a+4*d=0.
Now, we have to find ratio of m = 10th term and n = 20th term.
A[10]
= A + 9 * d
= A5 + 5 * d
= 0 + 5 * d
= 5 * d
Similarly, A[20]
= A + 19 * d
= A5 + 15 * d
= 0 + 15 * d
= 15 * d
Now, we have to find ratio, so Ans= A[10] / A[20]
Below is the required implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
void findRatio(ll m, ll n)
{
ll Am = m - 5, An = n - 5;
ll numerator = Am / (__gcd(Am, An));
ll denominator = An / (__gcd(Am, An));
cout << numerator << "/" << denominator << endl;
}
int main()
{
ll m = 10, n = 20;
findRatio(m, n);
return 0;
}
|
Java
public class GFG {
static int GCD( int a, int b) {
if (b== 0 ) return a;
return GCD(b,a%b);
}
static void findRatio( int m, int n)
{
int Am = m - 5 , An = n - 5 ;
int numerator = Am / GCD(Am, An) ;
int denominator = An / GCD(Am, An) ;
System.out.println(numerator + "/" + denominator);
}
public static void main (String args[]){
int m = 10 , n = 20 ;
findRatio(m, n);
}
}
|
Python3
from fractions import gcd
def findRatio(m,n):
Am = m - 5
An = n - 5
numerator = Am / / (gcd(Am,An))
denominator = An / / (gcd(Am, An))
print (numerator, '/' ,denominator)
if __name__ = = '__main__' :
m = 10
n = 20
findRatio(m, n)
|
C#
using System;
public class GFG {
static int GCD( int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
static void findRatio( int m, int n)
{
int Am = m - 5, An = n - 5 ;
int numerator = Am / GCD(Am, An) ;
int denominator = An / GCD(Am, An) ;
Console.Write(numerator + "/" + denominator);
}
public static void Main (){
int m = 10, n = 20;
findRatio(m, n);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0) return $a ;
return __gcd( $b , $a % $b );
}
function findRatio( $m , $n )
{
$Am = $m - 5; $An = $n - 5;
$numerator = $Am / (__gcd( $Am , $An ));
$denominator = $An / (__gcd( $Am , $An ));
echo $numerator , "/" ,
$denominator ;
}
$m = 10; $n = 20;
findRatio( $m , $n );
?>
|
Javascript
<script>
function GCD(a, b) {
if (b==0) return a;
return GCD(b,a%b);
}
function findRatio(m,n)
{
var Am = m - 5, An = n - 5 ;
var numerator = parseInt(Am / GCD(Am, An)) ;
var denominator = parseInt(An / GCD(Am, An));
document.write(numerator + "/" + denominator);
}
var m = 10, n = 20;
findRatio(m, n);
</script>
|
Time Complexity: O(log(max(m, n))), where m and n represents the given integers.
Auxiliary Space: O(log(max(m, n))) for call stack because using recursion