Adding Two Fractions and Displaying the Simplest Form

Adding Two Fractions and Displaying the Simplest Form

This article explains how to add two fractions and simplify the result into the lowest terms. This is a common problem in arithmetic, often solved using the Least Common Denominator (LCD) method and simplifying the result using the Greatest Common Divisor (GCD).Adding Two Fractions and Displaying the Simplest Form

Algorithm to Add Two Fractions

Given two fractions ab\frac{a}{b} and cd\frac{c}{d}:
  1. Calculate the numerator of the resulting fraction: x=(a×d)+(c×b)x = (a \times d) + (c \times b)
  2. Calculate the denominator of the resulting fraction: y=b×dy = b \times d
  3. Compute the GCD of xx and yy to simplify the fraction.
  4. Divide both numerator and denominator by the GCD to get the simplest form.

Python Program

python
from math import gcddef add_fractions(a, b, c, d): # Calculate numerator and denominator numerator = a * d + c * b denominator = b * d# Simplify using GCD common_divisor = gcd(numerator, denominator) numerator //= common_divisor denominator //= common_divisorreturn numerator, denominator# Input: Fractions a/b and c/d a, b = map(int, input("Enter numerator and denominator of first fraction (a b): ").split()) c, d = map(int, input("Enter numerator and denominator of second fraction (c d): ").split())# Add fractions result_numerator, result_denominator = add_fractions(a, b, c, d)# Output result print(f"The sum of the fractions is: {result_numerator}/{result_denominator}")

Examples

Input:

1 2 3 2

Output:

python
The sum of the fractions is: 2/1

Input:

1 3 3 9

Output:

python
The sum of the fractions is: 2/3

Explanation of Steps

  1. Finding Numerator and Denominator: For 12\frac{1}{2} and 32\frac{3}{2}:
    • Numerator: (1×2)+(3×2)=2+6=8(1 \times 2) + (3 \times 2) = 2 + 6 = 8
    • Denominator: 2×2=42 \times 2 = 4
  2. Simplify the Fraction:
    • GCD of 8 and 4 is 4.
    • Simplified Fraction: 84=2/1\frac{8}{4} = 2/1.
This article explains how to add two fractions and simplify the result into the lowest terms. This is a common problem in arithmetic, often solved using the Least Common Denominator (LCD) method and simplifying the result using the Greatest Common Divisor (GCD).

Algorithm to Add Two Fractions

Given two fractions ab\frac{a}{b} and cd\frac{c}{d}:
  1. Calculate the numerator of the resulting fraction: x=(a×d)+(c×b)x = (a \times d) + (c \times b)
  2. Calculate the denominator of the resulting fraction: y=b×dy = b \times d
  3. Compute the GCD of xx and yy to simplify the fraction.
  4. Divide both numerator and denominator by the GCD to get the simplest form.

Python Program

python
from math import gcddef add_fractions(a, b, c, d): # Calculate numerator and denominator numerator = a * d + c * b denominator = b * d# Simplify using GCD common_divisor = gcd(numerator, denominator) numerator //= common_divisor denominator //= common_divisorreturn numerator, denominator# Input: Fractions a/b and c/d a, b = map(int, input("Enter numerator and denominator of first fraction (a b): ").split()) c, d = map(int, input("Enter numerator and denominator of second fraction (c d): ").split())# Add fractions result_numerator, result_denominator = add_fractions(a, b, c, d)# Output result print(f"The sum of the fractions is: {result_numerator}/{result_denominator}")

Examples

Input:

1 2 3 2

Output:

python
The sum of the fractions is: 2/1

Input:

1 3 3 9

Output:

python
The sum of the fractions is: 2/3

Explanation of Steps

  1. Finding Numerator and Denominator: For 12\frac{1}{2} and 32\frac{3}{2}:
    • Numerator: (1×2)+(3×2)=2+6=8(1 \times 2) + (3 \times 2) = 2 + 6 = 8
    • Denominator: 2×2=42 \times 2 = 4
  2. Simplify the Fraction:
    • GCD of 8 and 4 is 4.
    • Simplified Fraction: 84=2/1\frac{8}{4} = 2/1.

Complexity Analysis

  1. Time Complexity: O(log⁡(min⁡(x,y)))O(\log(\min(x, y))), where xx and yy are the numerator and denominator.
  2. Space Complexity: O(1)O(1).

Suggested Visuals

  1. Diagram of Fraction Addition: Illustrate ab+cd\frac{a}{b} + \frac{c}{d} with labeled numerators and denominators.
  2. Simplification Process: Step-by-step illustration of calculating the GCD and dividing numerator/denominator.
Click here to know more our program! 
c