| by Arround The Web | No comments

Python Simplify Fractions

In mathematics, the fraction represents the two-values ratio performance, “numerator” and “denominator”, which are in the whole number. At times, it becomes necessary to simplify the fraction to facilitate easier comparisons and additions with other calculations and operations. Simplifying fractions means dividing the denominator and numerator by its GCD/greatest common divisor.

This guide will present various methods of simplifying the fractions using the below content:

Simplify Fractions Using fractions.Fraction() Method

To simplify fractions, the “Fraction()” method of the “fractions” module can be used in Python. Here is an example code that simplifies the fraction:

from fractions import Fraction
print(Fraction(24, 18))

The simplest form of the specified fraction is shown below:

Simplify Fractions Using Custom Function With math.gcd() Method

In Python, we can also simplify the fractions by creating the custom function and utilizing the “gcd()” method.

from math import gcd
def simp_fract(num1, num2):
    d = gcd(num1, num2)
    num1 = num1 // d
    num2 = num2 // d
    return num1,num2

num1 = 24
num2 = 18
print(simp_fract(num1, num2))

In this code:

  • The “gcd()” method is imported from the “math” module.
  • The custom function named “simp_fract” is defined with two arguments. The “num1” specifies the numerator value, and the “num2” indicates the denominator value.
  • Inside the function, the “gcd()” method retrieves the greatest common divisor of the specified numerator and denominator.
  • Next, the numerator and the denominator are divided by the greatest common divisor and return to the function.
  • Outside the function, the function is called on the two-parameter values and retrieves the value of the simplified fraction.


The fractions with simplified values have been retrieved successfully.

Simplify Fractions Using Custom Function With math.gcd() Method By Taking the User Input Value

The input user value can also be passed to the numerator and denominator value. Here in the below code, the “input()” function takes the user input data as string type and converts it into integers using the “int()” function:

from math import gcd
def reduceFraction(num1, num2):
    d = gcd(num1, num2)
    num1 = num1 // d
    num2 = num2 // d
    return num1,num2

num1 = int(input('Enter Numerator: '))
num2 = int(input('Enter Denominator: '))
print(reduceFraction(num1, num2))

The above code shows the following output:

Simplify Fractions Using Custom Function Along With Custom GCD Function

We can also define the custom “Greatest Common Divisor” instead of using the “gcd()” method of the “math” module. In the below code, we first define the “gcd” function, which retrieves the greatest common integers of the particular numerator and denominator of the fraction. Next, the simplifying fraction function is defined in a program that retrieves the fraction with the possible lowest fractions:

def gcd(num1, num2):
        while num2 != 0:
            t = num2
            num2 = num1%num2
            num1 = t
        return num1

def reducefract(num1, num2):
    return num1, num2

print(reducefract(30, 18))

The fraction with the simplified form is shown in the below output:


The “Fraction()” method of the “fractions” module, a custom function with the “gcd()” method, is used to simplify the specified fraction into the simplest form. We can also take/accept the data from the user and retrieve the fraction to the simplest form. This article delivered a detailed guide on Python to simplify fractions using several examples.

Share Button

Source: linuxhint.com

Leave a Reply