To reduce a fraction to its lowest terms you need to do the following steps:
Given the numerator and denominator of a fraction, determine their GCF by identifying the largest factor that is common between the two.
Divide the numerator and denominator by the (GCF) greatest common factor.
In this lab, you are going to define three functions: getFactors, gcf, and reduce.
Write the function getFactors that takes one integer parameter. The function should calculate and return a list of all of the factors of the parameter. For example, if getFactors was called by the following line of code:
getFactors(24)
it would return the following list of factors:
1,24
2,12
3,8
4,6
The return value of getFactors needs to be a list of tuples where each tuple stores a pair of factors, for example 3,8. You can use this code to test the function.
print() print("All the factors of 100") factors = getFactors(100) for f in factors: print(str(f[0]) + "," + str(f[1])) print() print() print("All the factors of 23") factors = getFactors(23) for f in factors: print(str(f[0]) + "," + str(f[1])) print()
Write the function gcf
that takes two parameters. The first parameter is an integer value representing the numerator of a fraction. The second parameter is an integer value representing the fraction's denominator.Follow these steps to complete the GCF function:
Use the getFactor function to get a list of the numerators factors. Store the list in a variable named numFactors.
Use the getFactor function to get a list of the denominators factors. Store the list in a variable named denomFactors.
Define an empty set named numFactorsSet.
Define an empty set named denomFactorsSet.
Recall that the getFactors function returns a list of tuples that contain factor pairs like 1,12 2,6 3,4. You need to split all the pairs apart so that each individual factor can be added to one of the sets. To do this, you will need to traverse the numFactors list using nested for loops. The outer loop should access each tuple in the list and then the inner loop should access each factor in the tuple. The factors should be added to the numFactorsSet.
Repeat the steps above for the denomFactors list. You should now have two sets named numFactorsSet and denomFactorsSet. numFactorsSet contains all of the factors of the numerator and denomFactorsSet contains all of the factors of the denominator.
The next step is to combine these two sets into one set that contains all of the factors that are in both sets giving you a set of common factors. To do this you are going to use the Set class's intersection method. The call to the intersection method is shown below.
commonFactors = numFactorsSet.intersection(denomFactorsSet)
The next step is to sort the common factors into either ascending or descending order. The code below uses the built-in function sorted. The function sorted by making a copy of commonFactors, converts the copy into a list, and then sorts the list in descending order. The default behavior of the sorted function is to sort the list in ascending order, so the parameter "reverse=True" was included so that it would instead sort in descending order.
factorsList = sorted(commonFactors, reverse=True)
Now that factorsList is sorted in descending order the greatest common factor is located at index 0. This code returns the gcf.
return factorsList[0]
The reduce function is responsible for doing the final step in the fraction reduction process. It divides the numerator by the GCF and divides the denominator by the GCF.
Complete the following steps to implement reduce:
Call the gcf function passing it the numerator and denominator from the parameter list and store the return value in a variable named GCF.
Divide the numerator by the GCF and store the result in a variable named numReduced.
Divide the denominator by the GCF and store the result in a variable named denomReduced.
Display the original fraction with a '/' between the numerator and denominator followed by the string " reduces to " then the reduced fraction with its numerator and denominator separated by a '/'. For example: 4/8 reduces to 1/2
Use the following code to test the reduce function.
reduce(50, 100) reduce(120, 38) reduce(12, 24) reduce(48, 64) reduce(13,17)
All the factors of 100 1,100 2,50 4,25 5,20 10,10 50/100 reduces to 1/2 120/38 reduces to 60/19 12/24 reduces to 1/2 48/64 reduces to 3/4 13/17 reduces to 13/17