# 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:

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.

#### 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
[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ display_to=”not_subscribed” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

Initial Challenge

#### Python Code

```#Maths Fraction Hack Algorithm - www.101computing.net/maths-fraction-hack-algorithm/

a = int(input("Enter the numerator of the first fraction:"))
b = int(input("Enter the denominator of the first fraction:"))
print("First Fraction: " + str(a) + "/" + str(b))

# 1 - Complete the code here to enter the second fraction
c = int(input("Enter the numerator of the second fraction:"))
d = int(input("Enter the denominator of the second fraction:"))
print("Second Fraction: " + str(c) + "/" + str(d))

# 2 - Complete the code here to calculate and compare the cross-products. Output which fraction is bigger
product1 = a*d
product2 = b*c
if product1 > product2:
print("The first fraction is bigger than the second one.")
elif product1 < product2:
print("The second fraction is bigger than the first one.")
else:
print("Both fractions are equivalent!")

# 3 - Complete the code here to output the decimal value of both fractions
print("First Fraction: " + str(a) + "/" + str(b) + " = " + str(a/b))
print("Second Fraction: " + str(c) + "/" + str(d) + " = " + str(c/d))

```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 3.4 / 5. Vote count: 29

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

As you found this challenge interesting...