Question

In: Computer Science

7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2...

7.


Fractions


You can express a fraction as a list: [numerator, denominator]. For example


1


2


can


be expressed as the list [1,2].


(a) Write a function called


factionAdd()


that takes two fractions as lists and adds


them. For example, fraction([1,2], [3,4]) returns [5,4]


(b) Write a function


fractionMult()


that multiplies two fractions that are passed


as lists.


[HINT: You may use the following function


gcd


(


x


;


y


) to help you calculate the


Greatest Common Divisor, which you will need to reduce fractions to the simplest


form.]


def gcd(x, y):


"""To calculate the greatest common divisor of x and y"""


x1 = abs(min(x, y))


y1 = abs(max(x, y))


gcd_ = x1


if y1 % x1:


gcd_ = gcd(x1, y1 % x1)


Solutions

Expert Solution

Code Screenshot :

Executable Code:

#Function to find the greatest common divisor
def gcd(x, y):
   #Find the absolute values
   x1 = abs(min(x, y))
   y1 = abs(max(x, y))
   gcd_ = x1
   #Computing the GCD
   if y1 % x1:
       gcd_ = gcd(x1, y1 % x1)
   #Return the result
   return gcd_

#Function to multiply fractions
def fractionMult(a,b):
   mul = (a[0]*b[0],a[1]*b[1])
   gcd_mul = gcd(mul[0],mul[1])
   #Finding the simplest form
   simplest_form= [mul[0]/gcd_mul,mul[1]/gcd_mul]
   #Return the result
   return simplest_form

#Function to add fractions
def fractionAdd(a,b):
   gcd_ab = gcd(a[1],b[1])
   m_a = b[1]/gcd_ab;
   m_b = a[1]/gcd_ab;
   sum = ((a[0]*m_a) + (b[0]*m_b),(gcd_ab*m_a*m_b))
   gcd_sum = gcd(sum[0],sum[1])
   #Finding the simplest form
   simplest_form = [sum[0]/gcd_sum,sum[1]/gcd_sum]
   #Return the result
   return simplest_form

#Prompting the user for input
n1 = int(input("Enter numerator for fraction 1: "))
d1 = int(input("Enter denominator for fraction 1: "))
n2 = int(input("Enter numerator for fraction 2: "))
d2 = int(input("Enter denominator for fraction 2: "))

#Printing the result
print("Addition of Fractions :",fractionAdd([n1,d1],[n2,d2]))
print("Product of Fractions :",fractionMult([n1,d1],[n2,d2]))

Sample Output :

Please comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks :)


Related Solutions

The denominator of a fraction is 4 more than the numerator. If both the numerator and...
The denominator of a fraction is 4 more than the numerator. If both the numerator and the denominator of the fraction are increased by 3, the new fraction is 5/6 . Find the original fraction.
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to do fraction arithmetic. To get you used to the idea of unit testing, this homework does not require a main method. You can create one if you find it useful, however, we will not be grading or even looking at that code. You should be comfortable enough with the accuracy of your test cases that you do not need to use print statements or...
1. Decomose the following fraction into partial fractions (3x)/(5x^(2)-x-6)
1. Decomose the following fraction into partial fractions (3x)/(5x^(2)-x-6)
How do you find the fraction between two fractions 1/4 and 3/5?
How do you find the fraction between two fractions 1/4 and 3/5?
Diet Fractions. Roll 2 dice and use the numbers to make a fraction less than or...
Diet Fractions. Roll 2 dice and use the numbers to make a fraction less than or equal to 1. Player A wins if the fraction cannot be reduced; otherwise, player B wins. a. Play the game 50 times and record the results. b. Is the game fair or not? Why or why not? c. Using the sample space for 2 dice, compute the probabilities of winning for player A and for player B. Do these agree with the results obtained...
When you convert feet to inches, how do you decide which part of the conversion factor should be in the numerator and which in the denominator?
When you convert feet to inches, how do you decide which part of the conversion factor should be in the numerator and which in the denominator?
Partial Fractions: Problem 2 Use the method of partial fraction decomposition to write the following rational...
Partial Fractions: Problem 2 Use the method of partial fraction decomposition to write the following rational expression as the sum of simpler rational functions whose denominators are polynomials of degree 1. −20x+20/x^2−x−56=
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT