In this challenge we are going to use an easy hack to quickly compare two fractions without having to calculate their decimal value.
For instance, can you quickly tell which of the following two fractions is bigger?
Fraction Hack: The Butterfly method!
Here is our quick approach to compare two fractions. It is called the butterfly method and is based on performing a cross-multiplication as follows:
The butterfly method is used to calculate the cross-products of our two fractions. By comparing these cross-products we can then deduct how both fractions compare:
Python Task
Your task is to write a Python program that will:
- Ask the user for to enter four values: the numerators and denominators of two fractions.
- Use the butterfly method to calculate and compare the cross products of these two fractions.
- Compare the cross products to decide which fraction is bigger and output a clear message on screen.
- Calculate and output the decimal values of both fractions to help the user confirm the outcome of the Butterfly method.
Python Code
Test Plan
Test # | Input Values | Expected Output | Actual Output |
#1 | 7/10 and 5/8 | 7/10 > 5/8 | |
#2 | 7/10 and 9/12 | 7/10 < 9/12 | |
#3 | 7/10 and 21/30 | 7/10 = 21/30 | |
#4 | 7/10 and 18/25 | 7/10 < 18/25 |
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area