Fractions S66556


Statement
 

pdf   zip

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.

Input

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)

Output

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

Observation

All computations must be carried out using only integers. Using reals (e.g. real division) will not work.

Public test cases
  • Input

    2 4 1 3 
    3 8 54 89 
    39 84  95 84 
    41 42 33 11
     2 6 31 8 
    74 94 1 69
    64 27 53 36
    

    Output

    2/4 + 1/3 = 5/6
    2/4 * 1/3 = 1/6
    2/4 > 1/3
    
    3/8 + 54/89 = 699/712
    3/8 * 54/89 = 81/356
    3/8 < 54/89
    
    39/84 + 95/84 = 67/42
    39/84 * 95/84 = 1235/2352
    39/84 < 95/84
    
    41/42 + 33/11 = 167/42
    41/42 * 33/11 = 41/14
    41/42 < 33/11
    
    2/6 + 31/8 = 101/24
    2/6 * 31/8 = 31/24
    2/6 < 31/8
    
    74/94 + 1/69 = 2600/3243
    74/94 * 1/69 = 37/3243
    74/94 > 1/69
    
    64/27 + 53/36 = 415/108
    64/27 * 53/36 = 848/243
    64/27 > 53/36
    
    
  • Input

    5 4 3 2
    
    12 24
    32 16
    
    6
    12 
    8 
    16
    
    11 11 22 23

    Output

    5/4 + 3/2 = 11/4
    5/4 * 3/2 = 15/8
    5/4 < 3/2
    
    12/24 + 32/16 = 5/2
    12/24 * 32/16 = 1/1
    12/24 < 32/16
    
    6/12 + 8/16 = 1/1
    6/12 * 8/16 = 1/4
    6/12 = 8/16
    
    11/11 + 22/23 = 45/23
    11/11 * 22/23 = 22/23
    11/11 > 22/23
    
    
  • Input

    1 99999999999999999 
    1 99999999999999998

    Output

    1/99999999999999999 + 1/99999999999999998 = 199999999999999997/9999999999999999700000000000000002
    1/99999999999999999 * 1/99999999999999998 = 1/9999999999999999700000000000000002
    1/99999999999999999 < 1/99999999999999998
    
    
  • Information
    Author
    Lluís Padró
    Language
    English
    Official solutions
    Python
    User solutions
    Python