What is Fraction Reduction to Lowest Term? | Maths Explanation for Perl Kids
Reducing fractions to their lowest terms is all about removing every common factor between the numerator and the denominator.
In this junior secondary Perl tutorial, you'll learn how to simplify fractions using a custom module. The Perl code provided helps reduce fractions to their lowest terms by finding common factors between the numerator and denominator. Whether you're a student or teacher, this math-focused Perl project makes fraction simplification easy and fun.
How the Reduce Fraction to Lowest Term Method Works | Detailed Explanation for Perl Kids
Let's see a Perl algorithm implementation to reduce a fraction to it's lowest term, using 12/16 as an example fraction.
A common factor to 12 and 16 is 2;
so removing 2 from 12/16 yields
6/8.
Again, 2 is common to both 6 and 8;
removing it yields 3/4.
We stop when nothing else is common (1 does not count).
Create a new Perl module file;
call it LowestTerm.pm.
Type out the adjoining Perl code for reducing fractions to their lowest terms.
Note: You can comment out the ImproperToMixed Perl object code in the main class from the previous lesson or simply continue from where it stopped.
So! Perl Fun Practice Exercise - Reduce Fraction to Lowest Term
As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the Perl code simplifies those fractions.
Perl Code for Reducing Fractions to Lowest Term - 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(reduceFraction);
}
use warnings;
use strict;
my ($numerator, $denominator, $trial_factor);
# simulate an object construct
# takes three arguments -- besides its name;
# whole_number, numerator and denominator values
sub new {
my $this = shift;
my $parameters = {@_};
bless $parameters, $this;
$this->_init(@_);
return $this;
}
# Simulate a constructor
sub _init {
my $self = shift;
($numerator, $denominator) = @_;
$trial_factor = $numerator < $denominator ? $numerator : $denominator;
}
# Returns a hash reference of the new numerator and denominator
sub reduceFraction {
# We are counting down to test for mutual factors
while ($trial_factor > 1) {
# do we have a factor
if (($numerator % $trial_factor) == 0) {
# is this factor mutual?
if (($denominator % $trial_factor) == 0) {
# where we have a mutual factor
$numerator /= $trial_factor;
$denominator /= $trial_factor;
next;
}
}
$trial_factor--;
}
return {numerator => $numerator, denominator => $denominator};
}
1;
Perl Code for Reducing Fractions to Lowest Term - Main Class
use strict;
use warnings;
use LOWESTTERM;
# Useful variables
my ($numerator, $denominator, $whole_number, $solution);
my (@numerators, @denominators, @solutions);
my (%fraction, %fractions);
##
# Reduce fractions to Lowest Term
#
$numerator = 16;
$denominator = 640;
print "\n To reduce to lowest term, simplifying:\n";
# Print as fraction
printf("%45u\n", $numerator);
printf("%45s\n", "-");
printf("%45u\n", $denominator);
# use the LowestTerm class
my $low_term = LOWESTTERM->new($numerator, $denominator);
%fraction = %{$low_term->reduceFraction()};
printf("\n%46u\n", $fraction{numerator});
printf("%46s\n", "Answer = -");
printf("%46u\n", $fraction{denominator});
print "\n\n";