Math: Add and Subtract Fractions with Unlike Denominators

Adding and Subtracting Fractions with Unlike Denominators in Python

Adding and Subtracting Fractions with Unlike Denominators in Python

This tutorial will guide you through adding and subtracting fractions that have different denominators using Python. We’ll explore the fundamental concept of finding a common denominator and then performing the arithmetic.

Understanding the Problem

When adding or subtracting fractions with different denominators, you first need to find a common denominator. The common denominator is a multiple of both denominators. Once you have a common denominator, you can rewrite the fractions so they have the same denominator and then add or subtract the numerators. The denominator remains the same.

Example 1: Simple Addition

Let’s start with a simple example: 1/2 + 1/3.


def add_fractions(num1, den1, num2, den2):
    """Adds two fractions with unlike denominators."""
    
    # Find the least common multiple (LCM) of the denominators
    lcm = (den1  den2) // gcd(den1, den2)  # Use gcd for efficiency
    
    # Rewrite the fractions with the common denominator
    new_num1 = num1  (lcm // den1)
    new_num2 = num2  (lcm // den2)
    
    # Add the numerators
    sum_num = new_num1 + new_num2
    
    # Simplify the fraction if possible (optional)
    common_divisor = gcd(sum_num, lcm)
    simplified_num = sum_num // common_divisor
    simplified_den = lcm // common_divisor
    
    return simplified_num, simplified_den

def gcd(a, b):
    """Calculates the greatest common divisor (GCD) using Euclid's algorithm."""
    while(b):
        a, b = b, a % b
    return a

# Example usage:
num1, den1 = 1, 2
num2, den2 = 1, 3
sum_num, sum_den = add_fractions(num1, den1, num2, den2)

print(f"The sum of {num1}/{den1} and {num2}/{den2} is {sum_num}/{sum_den}")

Explanation:

  • We define a function `add_fractions` that takes the numerator and denominator of two fractions as input.
  • Inside the function, we calculate the least common multiple (LCM) of the two denominators. We use the `gcd` function to efficiently calculate the LCM.
  • We rewrite both fractions with the common denominator by multiplying the numerator and dividing the denominator by the greatest common divisor.
  • We add the resulting numerators and return the simplified fraction as a tuple.

The `gcd` function uses Euclid’s algorithm to find the greatest common divisor. This is a more efficient way to calculate the LCM than simply multiplying the denominators.

Note: The LCM and GCD calculations prevent integer overflow issues, especially when dealing with larger numbers.

Output:

The sum of 1/2 and 1/3 is 5/6

Example 2: Simple Subtraction

Now let’s subtract 2/5 from 3/5:


def subtract_fractions(num1, den1, num2, den2):
    """Subtracts two fractions with unlike denominators."""
    
    # Find the least common multiple (LCM) of the denominators
    lcm = (den1  den2) // gcd(den1, den2)
    
    # Rewrite the fractions with the common denominator
    new_num1 = num1  (lcm // den1)
    new_num2 = num2  (lcm // den2)
    
    # Subtract the numerators
    diff_num = new_num1 - new_num2
    
    # Simplify the fraction if possible
    common_divisor = gcd(diff_num, lcm)
    simplified_num = diff_num // common_divisor
    simplified_den = lcm // common_divisor
    
    return simplified_num, simplified_den

def gcd(a, b):
    while(b):
        a, b = b, a % b
    return a

# Example usage:
num1, den1 = 3, 5
num2, den2 = 2, 5
diff_num, diff_den = subtract_fractions(num1, den1, num2, den2)

print(f"The difference of {num1}/{den1} and {num2}/{den2} is {diff_num}/{diff_den}")

Explanation:

  • Similar to Example 1, we define a function `subtract_fractions` to perform subtraction.
  • The key difference is that we subtract the numerators instead of adding them.
  • The rest of the process (calculating LCM, rewriting fractions, simplifying) remains the same.

Output:

The difference of 3/5 and 2/5 is 1/5

Example 3: Combining Addition and Subtraction

Let’s add 1/4 + 1/2 – 1/8:


def calculate_fraction(num1, den1, num2, den2):
    """Adds or subtracts two fractions with unlike denominators."""

    # Determine if we are adding or subtracting
    operation = "+" if num2 != 0 else "-"

    # Find the least common multiple (LCM) of the denominators
    lcm = (den1  den2) // gcd(den1, den2)

    # Rewrite the fractions with the common denominator
    new_num1 = num1  (lcm // den1)
    new_num2 = num2  (lcm // den2)
    
    # Perform the operation
    if operation == "+":
        result_num = new_num1 + new_num2
    else:
        result_num = new_num1 - new_num2

    # Simplify the fraction if possible
    common_divisor = gcd(result_num, lcm)
    simplified_num = result_num // common_divisor
    simplified_den = lcm // common_divisor

    return simplified_num, simplified_den

def gcd(a, b):
    while(b):
        a, b = b, a % b
    return a

# Example usage:
num1, den1 = 1, 4
num2, den2 = 1, 2
num3, den3 = 1, 8

result_num, result_den = calculate_fraction(num1, den1, num2, den2)
print(f"({num1}/{den1}) {operation} ({num2}/{den2}) = {result_num}/{result_den}")
result_num, result_den = calculate_fraction(result_num, result_den, num3, den3)
print(f"Result = {result_num}/{result_den}")

Explanation:

  • We modify the function to handle both addition and subtraction.
  • We determine the operation (+ or -) based on the input fractions.
  • The rest of the logic remains the same as in the previous examples.

Output:

 (1/4) + (1/2) = 3/4
Result = 1/4

Conclusion

You have now learned how to add and subtract fractions with unlike denominators using Python. Remember to always find a common denominator before performing the arithmetic operations. The use of the GCD function makes the process more efficient.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies and similar technologies to enhance your experience on wobizdu.com, analyze site traffic, personalize content, and deliver relevant ads. Some cookies are essential for the site to function, while others help us improve performance and user experience. You may accept all cookies, decline optional ones, or customize your settings. Review our Privacy Policy to learn more.