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.
Step 3:
Repeat Steps 1 & 2 recursively until there are no
more common factors.
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
package MULTIPLYFRACTION;
BEGIN {
require Exporter;
our $VERSION = 2016.12;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(doConvert);
}
use warnings;
use strict;
my ($trial_factor, $n_index, $d_index, $mutual_factor);
my (@numerators, @denominators, @answer);
my %fractions;
sub new {
no warnings "all";
my $this = shift;
my $parameters = shift;
bless $parameters, $this;
$this->_init($parameters);
return $this;
}
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; }
}
sub doMultiply {
while ($trial_factor > 1) {
while ($n_index < scalar @numerators) {
$mutual_factor = 0;
if (($numerators[$n_index] % $trial_factor) == 0) {
while ($d_index < scalar @denominators) {
if (($denominators[$d_index] % $trial_factor) == 0) {
$mutual_factor = 1;
last;
}
$d_index++;
}
last;
}
$n_index++;
}
if ($mutual_factor) {
$numerators[$n_index] /= $trial_factor;
$denominators[$d_index] /= $trial_factor;
next;
}
$n_index = 0;
$d_index = 0;
$trial_factor--;
}
for (0 .. $
$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;
my (@numerators, @denominators);
my (%fraction, %fractions);
@numerators = (16, 20, 27, 20);
@denominators = (9, 9, 640, 7);
%fractions = (
numerators => \@numerators,
denominators => \@denominators
);
print "\n Solving:\n";
printf("%13u", $_) for @numerators;
printf("\n%12s", " ");
print "- X " for 1 .. $
printf("%1s", "-");
print "\n";
printf("%13u", $_) for @denominators;
print "\n";
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";