Understanding Fractional Numbers | Maths Explanation for Perl Kids
Welcome to this junior secondary Perl math project! In this tutorial, you'll learn how to
convert improper fractions to mixed fractions using Perl. This beginner-friendly guide is perfect for students
exploring math with Perl, and includes a simple Perl script to help you understand fraction conversion step by step.
Whether you're just starting out with Perl or looking for a fun math coding activity,
this project will show you how to build a mixed fraction calculator using basic programming concepts.
Coding fractions in Perl is a fun way to combine math and programming.
This project helps junior secondary students build confidence in both areas, and also teaches how to apply math logic in Perl code.
As before, fractional numbers can either be
Proper (e.g. 2/5),
Improper (e.g. 5/2), or
Mixed (e.g. 21/2),
Step-by-Step Explanation of Improper Fraction to Mixed Fraction Conversion in Perl
Say we have the fraction 10/3 -
which is an improper fraction - we can write a Perl algorithm that converts this improper fraction
to a mixed fraction by following a number of steps:
Step 1:
Find the largest number smaller than the numerator (10),
that can divide the denominator - 3 - without remainder.
⇒ 9
Step 2:
Subtract the found number (9) - from Step 1 -
from the numerator (10)
⇒ 10 - 9 = 1;
This yields the whole number.
Step 3:
Divide the found number (9) - from Step 1 -
by the denominator (3)
⇒ 9/3 = 3;
This yields the new numerator.
Step 4:
Reuse the denominator (3) to get our mixed fraction
⇒ 31/3
Create a new perl module file;
call it ImproperToMixed.pm
Type out the adjoining Perl code for converting improper to mixed fractions.
Note: You can comment out the MixedToImproper Perl object
code in the main class from the previous lesson or simply continue from where it stopped.
So! Perl Fun Practice Exercise - Convert Improper Fraction to Mixed Fraction
As a fun practice exercise, feel free to try out your own improper fractions with different numerators and denominators,
and see how the Perl code simplifies those fractions.
Perl Code for Converting Improper Fraction To Mixed Fraction - Module File
package IMPROPERTOMIXED;
BEGIN {
require Exporter;
our $VERSION = 2016.12;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(doConvert);
}
use warnings;
use strict;
my ($whole_number, $numerator, $denominator);
sub new {
no warnings "all";
my $this = shift;
my $parameters = {@_};
bless $parameters, $this;
$this->_init(@_);
return $this;
}
sub _init {
my $self = shift;
($numerator, $denominator) = @_;
}
sub doConvert {
my $new_numerator;
my $whole_number;
for (reverse 2 .. $numerator - 1) {
if (($_ % $denominator) == 0) {
$new_numerator = $numerator - $_;
$whole_number = $_ / $denominator;
last;
}
}
return {numerator => $new_numerator, whole_number => $whole_number};
}
1;
Perl Code for Converting Improper Fraction To Mixed Fraction - Algebra Main class
use strict;
use warnings;
use IMPROPERTOMIXED;
my ($numerator, $denominator, $whole_number, $solution);
my %fraction;
$numerator = 10;
$denominator = 3;
print "\n Converting from Improper to Mixed the fraction:\n";
printf("%55u\n", $numerator);
printf("%55s\n", "-");
printf("%55u\n", $denominator);
my $imp2mix = IMPROPERTOMIXED->new($numerator, $denominator);
%fraction = %{$imp2mix->doConvert()};
printf("\n%52u\n", $fraction{numerator});
printf("%50s%u%s\n", "Answer = ", $fraction{whole_number}, "-");
printf("%52u\n", $denominator);
print "\n\n";