Understanding the Math Behind Fraction Multiplication | Maths Explanation for Perl Kids
Learning how to multiply fractions in Perl is a great way to combine math skills with coding. This tutorial is designed for junior secondary students who want to understand fraction multiplication using simple Perl classes and constructors.
Multiplying fractions is pretty straightforward:
Cancel out all common factors between numerators and denominators,
then multiply whatever is left numerator to numerator and
denominator to denominator.
In this lesson, we'll walk through the step-by-step method of multiplying fractions using Perl. You'll learn how to define a class, use constructors, and apply logic to find mutual factors and simplify results.
Step-by-Step Explanation of Algorithm to Multiply Fractions in Perl
This Perl algorithm for fractions shows how to multiply two fractions and reduce them to their lowest terms.
It's a great math coding project for beginners.
Understanding how to multiply multiple fractions in Perl helps students build both computational thinking and math fluency.
It's a foundational skill for more advanced topics like algebra and data science.
If we have
4/9 x 21/8;
Step 1:
Find any common factor between any numerator and any denominator.
Step 2:
Cancel out any such common factor.
| = | X | 7 | |
| 4 | 21 | ||
| 9 | 8 | ||
| 3 |
Step 3:
Repeat Steps 1 & 2 recursively until there are no more common factors.
| = | 1 | X | |
| 4 | 7 | ||
| 3 | 8 | ||
| 2 |
| = | 7 |
| 6 |
Create a new Perl module file;
call it MultiplyFraction.pm.
Type out the adjoining Perl code for multiplying fractions.
Note: You can comment out the LowestTerm Perl object code in the main class from the previous lesson or simply continue from where it stopped.
So! Perl Fun Practice Exercise - Multiply 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 multiplies those fractions.
Perl Code for Multiplying Fractions - Module File
BEGIN {
require Exporter;
# for the sake of standard
our $VERSION = 2016.12;
# Inherit from exporter to export functions and variables
our @ISA = qw(Exporter);
# Functions and variables to be exported by default
our @EXPORT_OK = qw(doConvert);
}
use warnings;
use strict;
my ($trial_factor, $n_index, $d_index, $mutual_factor);
my (@numerators, @denominators, @answer);
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}};
$trial_factor = 0;
$n_index = 0;
$d_index = 0;
$answer[0] = 1;
$answer[1] = 1;
for (@numerators) { $trial_factor = $_ if $_ > $trial_factor; }
for (@denominators) { $trial_factor = $_ if $_ > $trial_factor; }
}
# Returns a hash reference of the new numerator and denominator
sub doMultiply {
# STEP 3:
# We are counting down to test for mutual factors
while ($trial_factor > 1) {
# STEP 1:
# iterate through numerators and check for factors
while ($n_index < scalar @numerators) {
$mutual_factor = 0;
if (($numerators[$n_index] % $trial_factor) == 0) { # do we have a factor
# iterate through denominators and check for factors
while ($d_index < scalar @denominators) {
if (($denominators[$d_index] % $trial_factor) == 0) { # is this factor mutual?
$mutual_factor = 1;
last; # stop as soon as we find a mutual factor so preserve the corresponding index
}
$d_index++;
}
last; # stop as soon as we find a mutual factor so as to preserve the corresponding index
}
$n_index++;
}
# STEP 2:
# where we have a mutual factor
if ($mutual_factor) {
$numerators[$n_index] /= $trial_factor;
$denominators[$d_index] /= $trial_factor;
next; # continue with next iteration repeating the current value of trial_factor
}
$n_index = 0;
$d_index = 0;
$trial_factor--;
}
for (0 .. $#numerators) {
$answer[0] *= $numerators[$_];
$answer[1] *= $denominators[$_];
}
return {numerator => $answer[0], denominator => $answer[1]};
}
1;
Perl Code for Multiplying Fractions - Main Class
use strict;
use warnings;
use MULTIPLYFRACTION;
# Useful variables
my (@numerators, @denominators);
my (%fraction, %fractions);
##
# Multiplying fractions
##
@numerators = (16, 20, 27, 20);
@denominators = (9, 9, 640, 7);
%fractions = (
numerators => \@numerators,
denominators => \@denominators
);
print "\n Solving:\n";
# Print as fraction
printf("%13u", $_) for @numerators;
printf("\n%12s", " ");
print "- X " for 1 .. $#numerators;
printf("%1s", "-");
print "\n";
printf("%13u", $_) for @denominators;
print "\n";
# use the MultiplyFraction class
my $mul_fract = MULTIPLYFRACTION->new(\%fractions);
%fraction = %{$mul_fract->doMultiply()};
printf("\n%25u\n", $fraction{numerator});
printf("%25s\n", "Answer = -");
printf("%25u\n", $fraction{denominator});
print "\n\n";