Product of given N fractions in reduced form
Given the Numerator and Denominator of N fractions. The task is to find the product of N fraction and output the answer in reduced form.
Examples:
Input : N = 3
num[] = { 1, 2, 5 }
den[] = { 2, 1, 6 }
Output : 5/6
Product of 1/2 * 2/1 * 5/6 is 10/12.
Reduced form of 10/12 is 5/6.
Input : N = 4
num[] = { 1, 2, 5, 9 }
den[] = { 2, 1, 6, 27 }
Output : 5/18
The idea is to find the product of Numerator in a variable, say new_num. Now, find the product of Denominator in another variable, say new_den.
Now, to find the answer in Reduced form, find the Greatest Common Divisor of new_num and new_den and divide the new_num and new_den by the calculated GCD.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
void productReduce( int n, int num[], int den[])
{
int new_num = 1, new_den = 1;
for ( int i = 0; i < n; i++) {
new_num *= num[i];
new_den *= den[i];
}
int GCD = gcd(new_num, new_den);
new_num /= GCD;
new_den /= GCD;
cout << new_num << "/" << new_den << endl;
}
int main()
{
int n = 3;
int num[] = { 1, 2, 5 };
int den[] = { 2, 1, 6 };
productReduce(n, num, den);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static void productReduce( int n, int num[],
int den[])
{
int new_num = 1 , new_den = 1 ;
for ( int i = 0 ; i < n; i++) {
new_num *= num[i];
new_den *= den[i];
}
int GCD = gcd(new_num, new_den);
new_num /= GCD;
new_den /= GCD;
System.out.println(new_num + "/" +new_den);
}
public static void main (String[] args) {
int n = 3 ;
int num[] = { 1 , 2 , 5 };
int den[] = { 2 , 1 , 6 };
productReduce(n, num, den);
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b;
return gcd(b % a, a);
def productReduce(n, num, den):
new_num = 1 ;
new_den = 1 ;
for i in range (n):
new_num = new_num * num[i];
new_den = new_den * den[i];
GCD = gcd(new_num, new_den);
new_num = new_num / GCD;
new_den = new_den / GCD;
print ( int (new_num), "/" ,
int (new_den));
n = 3 ;
num = [ 1 , 2 , 5 ];
den = [ 2 , 1 , 6 ];
productReduce(n, num, den);
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static void productReduce( int n, int []num,
int []den)
{
int new_num = 1, new_den = 1;
for ( int i = 0; i < n; i++) {
new_num *= num[i];
new_den *= den[i];
}
int GCD = gcd(new_num, new_den);
new_num /= GCD;
new_den /= GCD;
Console.WriteLine(new_num + "/" +new_den);
}
public static void Main () {
int n = 3;
int []num = { 1, 2, 5 };
int []den = { 2, 1, 6 };
productReduce(n, num, den);
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function productReduce( $n , $num , $den )
{
$new_num = 1; $new_den = 1;
for ( $i = 0; $i < $n ; $i ++)
{
$new_num *= $num [ $i ];
$new_den *= $den [ $i ];
}
$GCD = gcd( $new_num , $new_den );
$new_num /= $GCD ;
$new_den /= $GCD ;
echo $new_num , "/" , $new_den , "\n" ;
}
$n = 3;
$num = array (1, 2, 5);
$den = array (2, 1, 6);
productReduce( $n , $num , $den );
?>
|
Javascript
<script>
function gcd( a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function productReduce( n, num , den)
{
let new_num = 1, new_den = 1;
for (let i = 0; i < n; i++) {
new_num *= num[i];
new_den *= den[i];
}
let GCD = gcd(new_num, new_den);
new_num /= GCD;
new_den /= GCD;
document.write( new_num + "/" + new_den);
}
let n = 3;
let num = [ 1, 2, 5 ];
let den = [ 2, 1, 6 ];
productReduce(n, num, den);
</script>
|
Output :
5/6
Time Complexity: O(n + log(min(a,b)))
Auxiliary Space: O(log(min(a,b)))
How to avoid overflow?
The above solution causes overflow for large numbers. We can avoid overflow by first finding prime factors of all numerators and denominators. Once we have found prime factors, we can cancel common prime factors.
Note : When you are asked to represent the answer in form of {P \times {Q} ^ {-1}} .
For these questions, first convert the numerator and denominator in reducible form P / Q as explained above. Then, find modular multiplicative inverse of Q with respect to a prime number m (Generally, 10^9 + 7) which is given in question. After finding modular multiplicative inverse of Q, multiply it with P and take modulo with given prime number m which gives us our required output.
// Thanks VaiBzZk for suggesting this condition.
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...