Maths Fraction Hack Algorithm

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:

  1. Ask the user for to enter four values: the numerators and denominators of two fractions.
  2. Use the butterfly method to calculate and compare the cross products of these two fractions.
  3. Compare the cross products to decide which fraction is bigger and output a clear message on screen.
  4. 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
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.5 / 5. Vote count: 28

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!