Write a program that reads pairs of fractions (given as four
integers: num1 den1 num2 den2) and for each
pair writes its sum, its product, and a comparison of both
fractions.
First, define the following functions
def simplify(n,d):
'''
- Receives the numerator and denominator of a fraction
- Returns the numerator and denominator of the simplified equivalent fraction
'''
def sumfrac(n1,d1,n2,d2) :
'''
- Receives the numerator and denominator of two fraction
- Returns the numerator and denominator of the simplified sum fraction
'''
def prodfrac(n1,d1,n2,d2) :
'''
- Receives the numerator and denominator of two fraction
- Returns the numerator and denominator of the simplified product fraction
'''
def smaller((n1,d1,n2,d2) :
'''
- Receives the numerator and denominator of two fraction
- Returns True if n1/d1 < n2/d2, False otherwise
'''
You can use the following function that computes the GCD of two numbers:
def gcd(a,b):
'''
- Receives two integers a>0, b>0
- Returns the greatest common divisor of a and b
'''
if a<b :
a,b = b,a
while b>0:
a, b = b, a%b
return a
Your main program must use these functions. You can not alter the name or parameters of the given functions.
The input consists of a sequence of groups of 4 integers. Not necessarily each group of four integers will be in a single line (see the examples)
Each group is to be interpreted as two fractions
num1 den1 num2 den2, and for each group, the
sum and product of the two fractions must be printed as simplified
fractions, as well as a comparison. Follow the format of the
examples
All computations must be carried out using only integers. Using reals (e.g. real division) will not work.
Author: Lluís Padró
Generation: 2026-07-06T09:10:22.790Z
© Jutge.org, 2006–2026.
https://jutge.org