Python: Math – Add and Subtract Fractions with Unlike Denominators

Python: Math – Add and Subtract Fractions with Unlike Denominators

Python: Math – Add and Subtract Fractions with Unlike Denominators

This tutorial will guide you through adding and subtracting fractions with unlike denominators using Python. We’ll build upon simple examples to gradually incorporate more complex calculations.

Understanding the Problem

Adding and subtracting fractions requires that they have a common denominator. The denominator of a fraction represents the total number of equal parts in the whole. When the denominators are different, we need to find a common denominator (a multiple of both) before we can add or subtract.

Example 1: Basic Addition with Common Denominator (Already Done)

Let’s start with a simple case where the fractions already have a common denominator, so we only need to add the numerators.


def add_fractions_same_denominator(numerator1, denominator1, numerator2, denominator2):
  """Adds two fractions with the same denominator."""
  result_numerator = numerator1 + numerator2
  result_denominator = denominator1
  return result_numerator, result_denominator

# Example usage
numerator1 = 1
denominator1 = 2
numerator2 = 1
denominator2 = 2

result_numerator, result_denominator = add_fractions_same_denominator(numerator1, denominator1, numerator2, denominator2)
print(f"The sum is {result_numerator}/{result_denominator}")

In this example, the denominators are both 2, so we simply add the numerators (1 + 1 = 2) and keep the denominator as 2. The result is 2/2, which simplifies to 1.

Note: This example only demonstrates a very simple scenario. In reality, you’d need to find the least common multiple (LCM) of the denominators to find a common denominator that isn’t immediately obvious.

Output:


The sum is 2/2

Example 2: Finding a Common Denominator and Adding

Now, let’s handle fractions with different denominators. We’ll first find a common denominator, then add the numerators.


from math import gcd  # Import gcd function to find greatest common divisor

def find_lcm(a, b):
    """Calculates the least common multiple (LCM) of two numbers."""
    return (a  b) // gcd(a, b)

def add_fractions_unlike_denominator(numerator1, denominator1, numerator2, denominator2):
    """Adds two fractions with unlike denominators."""
    common_denominator = find_lcm(denominator1, denominator2)
    result_numerator = (numerator1  (common_denominator // denominator1)) + (numerator2  (common_denominator // denominator2))
    result_denominator = common_denominator
    return result_numerator, result_denominator


# Example usage
numerator1 = 1
denominator1 = 3
numerator2 = 1
denominator2 = 4

result_numerator, result_denominator = add_fractions_unlike_denominator(numerator1, denominator1, numerator2, denominator2)
print(f"The sum is {result_numerator}/{result_denominator}")

Explanation:

  1. We import the `gcd` (greatest common divisor) function from the `math` module.
  2. The `find_lcm` function calculates the least common multiple using the formula: LCM(a, b) = (a b) / GCD(a, b).
  3. The `add_fractions_unlike_denominator` function takes the numerators and denominators of the two fractions as input.
  4. It calculates the common denominator using `find_lcm`.
  5. Then, it converts each fraction to an equivalent fraction with the common denominator, adds the numerators, and keeps the common denominator.

Let’s trace the example:

denominator1 = 3, denominator2 = 4

LCM(3, 4) = 12

numerator1 = 1, denominator1 = 3 => 1 / 3 = 1 4 / 3 4 = 4/12

numerator2 = 1, denominator2 = 4 => 1 / 4 = 1 3 / 4 3 = 3/12

4/12 + 3/12 = 7/12

Output:


The sum is 7/12

Example 3: Subtracting Fractions with Unlike Denominators


from math import gcd

def find_lcm(a, b):
    """Calculates the least common multiple (LCM) of two numbers."""
    return (a  b) // gcd(a, b)

def subtract_fractions_unlike_denominator(numerator1, denominator1, numerator2, denominator2):
    """Subtracts two fractions with unlike denominators."""
    common_denominator = find_lcm(denominator1, denominator2)
    result_numerator = (numerator1  (common_denominator // denominator1)) - (numerator2  (common_denominator // denominator2))
    result_denominator = common_denominator
    return result_numerator, result_denominator

# Example usage
numerator1 = 5
denominator1 = 6
numerator2 = 2
denominator2 = 3

result_numerator, result_denominator = subtract_fractions_unlike_denominator(numerator1, denominator1, numerator2, denominator2)
print(f"The difference is {result_numerator}/{result_denominator}")

The logic is very similar to the addition example, but we use subtraction instead of addition. The steps are:

  1. Find the LCM of the denominators.
  2. Convert each fraction to an equivalent fraction with the LCM as the denominator.
  3. Subtract the numerators.
  4. Keep the LCM as the denominator.

Output:


The difference is 11/6

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.