Why Rationalise or Canonise Fractions before Addition | Maths Explanation for Python Kids
In this Python tutorial for junior secondary students, we explore how to add fractions. Before performing the addition,
we rationalise or canonise the fractions to ensure accuracy. This method uses Finding LCM in Python
class to align denominators, making it ideal for math programming beginners.
This Python tutorial teaches young students how to add fractions with different denominators.
Before fractions are added, they are rationalised; i.e., they are put in a form where their denominators become
the same. This identical denominator is the LCM of the previous denominators of all the separate fractions.
After this is done, the new numerators can then be added together.
Step-by-Step Guide for Addition of Fractions - Python Algorithm
The following steps will guide us in writing our Python code for adding fractions.
Let's illustrate the steps to follow with the example fractional expression
2/5 + 7/4
Step 1:
Using the Find LCM in Python
class from the Primary Category, find the LCM of the denominators.
⇒ LCM of 5 & 4 = 20
Step 2:
In a turn by turn fashion, divide the found LCM from Step 1
by each denominator, multiplying the quotient by the corresponding numerator.
⇒
((2 x 4) + (7 x 5))/20
= (8 + 35)/20
Step 3:
Go ahead and add the numerators.
⇒
43/20
Create a new Python module file;
Call it AddFraction.pm.;
Type out the adjoining Python code for adding fractions.
Note: The code module for Learn how to find LCM in Python
is from the Primary Category.
Just make a copy of it into the current folder (project).
You can comment out the DivideFraction Python object
code in the main class from the previous lesson or simply continue from where it stopped.
So! Python Fun Practice Exercise - Add Fractions
As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the Python code adds these fractions.
Python Code for Add Fractions - Module File
class PlusFraction:
# Simulate a constructor
def __init__(self, fractions):
self.numerators = fractions['numerators']
self.denominators = fractions['denominators']
self.answer = 0
self.new_numerators = []
# transforms fractions so they all have same denominator
# takes a dictionary of a list of numerators and denominators
#
# returns a dictionary of the new numerators and new denominator(LCM)
def canoniseFraction(self):
# STEP 1:
from LCM import findLCM
self.l_c_m = findLCM(self.denominators)
self.lcm = self.l_c_m.getLCM()
# STEP 2:
# make numerators vary(ratio) with lcm
for count in range(len(self.denominators)):
self.new_numerators.append(int(self.lcm / self.denominators[count] * self.numerators[count]))
return None
# returns a dictionary of the
# new numerator and new denominator(LCM)
def doAdd(self):
self.canoniseFraction()
# STEP 3:
# add all transformed numerators
for num in self.new_numerators: self.answer += num
return {'numerator':self.answer, 'denominator':self.lcm}
Python Code for Add Fractions - Main Class
from AddFraction import PlusFraction
##
# Adding fractions
##
numerators = [1, 1, 1, 1]
denominators = [4, 4, 4, 4]
fractions = {'numerators':numerators, 'denominators':denominators}
print('\n Solving:\n')
# Print as fraction
for numerator in fractions['numerators']: print('{:13d}'.format(numerator), end='')
print('{}{:>12}'.format('\n', ' '), end='')
for wasted in range(len(numerators)-1): print('{}'.format('- + '), end='')
print('{:>1}'.format('-'))
for denominator in fractions['denominators']: print('{:13d}'.format(denominator), end='')
print('\n\n')
# use the AddFraction class
add_fract = PlusFraction(fractions)
fraction = add_fract.doAdd()
print('{:25d}'.format(fraction['numerator']))
print('{:>25}'.format('Answer = -'))
print('{:25d}'.format(fraction['denominator']))
print('\n\n')