subtracting fractions with Different Denominators | Rationalising Fractions in Perl
Learning how to subtract fractions in Perl is an important step for junior secondary students who are beginning to combine mathematics with coding. In this tutorial, we'll walk through the process of subtracting fractions with different denominators, explain how to use the LCM (Lowest Common Multiple) method, and show you how to rationalise or canonise fractions before subtraction. By the end, you'll be able to write a simple Perl program to subtract fractions step by step, giving you both a solid maths foundation and practical coding skills to implement libraries like the built-in Perl fractions module.
Like with Adding Fractions in Perl, fractional numbers are also rationalised before subtraction.
This means 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 subtracted.
Steps for Subtraction of Fractions with Different Denominators - Perl Algorithm
The following steps will guide us in writing our Perl code for subtracting fractions.
Let's illustrate these steps with the example fractional expression
7/4 - 2/5
Step 1:
Using the Find LCM in Perl
class from the Primary Category, find the LCM of the denominators.
⇒ LCM of 4 & 5 = 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.
⇒
((7 x 5) - (2 x 4))/20
= (35 - 8)/20
Step 3:
Go ahead and subtract the numerators.
⇒
27/20
Create a new Perl module file;
call it SubtractFraction.pm
Type out the adjoining Perl code for subtracting fractions.
Note: You can comment out the AddFraction Perl object code in the main class from the previous lesson or simply continue from where it stopped.
So! Perl 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 Perl code divides those fractions.
Perl Code for Subtracting Fractions - Module File
BEGIN {
require Exporter;
# for the sake of standard
our $VERSION = 2016.12;
# Functions and variables to be exported by default
our @EXPORT_OK = qw(doSubtract);
}
use warnings;
use strict;
use parent "ADDFRACTION";
my $answer;
my (@numerators, @denominators, @new_numerators);
my %fractions;
# simulate an object construct
# takes one argument -- besides its package name;
# hash reference to array references for numerators and denominators
sub new {
no warnings "all";
my $this = shift;
my $parameters = shift; #this is already a hash reference
bless $parameters, $this;
$this->_init($parameters);
return $this;
}
# Simulate a constructor
sub _init {
my $self = shift;
my $aux = shift;
$fractions{numerators} = $aux->{numerators};
$fractions{denominators} = $aux->{denominators};
@numerators = @{$fractions{numerators}};
@denominators = @{$fractions{denominators}};
$answer = 0;
}
# returns an array --
# new numerator and new denominator(LCM) in that order
sub doSubtract {
# STEPS 1, 2:
my $call = ADDFRACTION->new(\%fractions);
my @help = $call->canoniseFraction();
@new_numerators = @{$help[0]};
# STEP 3:
# subtract all transformed numerators
$answer = $new_numerators[0];
$answer -= $new_numerators[$_] for 1 .. $#new_numerators;
return ($answer, $help[1]);
}
1;
Perl Code for Subtracting Fractions - Main Class
use strict;
use warnings;
use SUBTRACTFRACTION;
# Useful variables
my (@numerators, @denominators, @solutions);
my %fractions;
##
# Subtracting fractions
##
@numerators = (9, 3, 5, 7);
@denominators = (2, 4, 12, 18);
%fractions = (
numerators => \@numerators,
denominators => \@denominators
);
print "\n Solving:\n";
# Print as fraction
printf("%13u", $_) for @numerators;
printf("\n%12s", " ");
print "- - " for 1 .. $#numerators;
printf("%2s\n", "-");
printf("%13u", $_) for @denominators;
print "\n";
# use the SubtractFraction class
my $sub_fract = SUBTRACTFRACTION->new(\%fractions);
@solutions = $sub_fract->doSubtract();
printf("\n%25u\n", $solutions[0]);
printf("%25s\n", "Answer = -");
printf("%25u\n", $solutions[1]);
print "\n\n";