usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

How to Reduce Fractions to their Lowest Term in Python | Kids Fun Project



What is Fraction Reduction to Lowest Term? | Maths Explanation for Python Kids

Reducing fractions to their lowest terms is all about removing every common factor between the numerator and the denominator.

In this junior secondary Python tutorial, you'll learn how to simplify fractions using a custom module. The Python code provided helps reduce fractions to their lowest terms by finding common factors between the numerator and denominator. Whether you're a student or teacher, this math-focused Python project makes fraction simplification easy and fun.


How the Reduce Fraction to Lowest Term Method Works | Detailed Explanation for Python Kids

Let's see a Python algorithm implementation to reduce a fraction to it's lowest term, using 12/16 as an example fraction.

A common factor to 12 and 16 is 2;
so removing 2 from 12/16 yields 6/8.

Again, 2 is common to both 6 and 8;
removing it yields 3/4.

We stop when nothing else is common (1 does not count).

Create a new Python module file; call it LowestTerm.py.
Type out the adjoining Python code for reducing fractions to their lowest terms.


Note: You can comment out the ImproperToMixed Python object code in the main class from the previous lesson or simply continue from where it stopped.


So! Python Fun Practice Exercise - Reduce Fraction to Lowest Term

As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the Python code simplifies those fractions.







Python Code for Reducing Fractions to Lowest Term - Module File

# A class
class LowTerm:

    # A constructor
    def __init__(self, fraction):
        self.numerator   = fraction['numerator']
        self.denominator = fraction['denominator']

        # set trial_factor to the smaller value between numerator and denominator
        if self.numerator < self.denominator: self.trial_factor = self.numerator
        else: self.trial_factor = self.denominator

    # Returns a dictionary of the new numerator and denominator
    def reduceFraction(self):
        # We are counting down to test for mutual factors 
        while self.trial_factor > 1:
            # do we have a factor
            if self.numerator % self.trial_factor == 0:
                # is this factor mutual?
                if self.denominator % self.trial_factor == 0:
                    # where we have a mutual factor
                    self.numerator   /= self.trial_factor
                    self.denominator /= self.trial_factor
                    continue

            self.trial_factor -= 1

        return {'numerator':int(self.numerator), 'denominator':int(self.denominator)}


Python Code for Reducing Fractions to Lowest Term - Main Class

#!/usr/bin/python
from LowestTerm import LowTerm

##
 # Reduce fractions to Lowest Term
 #
fraction = {'numerator':16, 'denominator':640}

print('\n    To reduce to lowest term, simplifying:\n')
# Print as fraction
print('{:45d}'.format(fraction['numerator']))
print('{:>45}'.format('-'))
print('{:45d}'.format(fraction['denominator']))

# use the LowestTerm class
low_term = LowTerm(fraction)
fraction = low_term.reduceFraction()

print('{:46d}'.format(fraction['numerator']))
print('{:>46}'.format('Answer =    -'))
print('{:46d}'.format(fraction['denominator']))


print('\n\n')




<< Previous Next >>