usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

How to Sort Fractions in Ascending and Descending Orders using Perl | Step-by-Step Tutorial for Students



Why Sorting Fractions Matters in Math and Perl

Sorting fractions in Perl is a valuable skill for junior secondary students who are learning to connect mathematics with coding. Instead of simply converting fractions into decimals, this tutorial shows you how to compare and arrange fractions step by step using the LCM method. By the end, you'll know how to write a Perl program to sort fractions in ascending and descending order, making it easier to solve math problems and strengthen your programming logic.

In this guide, we'll start with the basics of comparing fractions, then move on to a clear Perl code example for sorting fractions. You'll see how to rationalise fractions, apply the least common multiple (LCM), and use Perl's built-in functions to arrange them correctly. This approach not only improves your understanding of fractions but also builds confidence in writing simple algorithms.

Whether you're a student practicing for class, a teacher preparing classroom exercises, or a beginner looking for a Perl fractions tutorial, this lesson is designed to be easy to follow. Let's dive in and learn how to sort fractions step by step with Perl.


Rationalise (Canonise) the Fractions before Sorting | Maths Explanation for Perl Kids

Just like was shown with Adding Fractions in Perl and Subtractinging Fractions in Perl, fractional numbers also have to be rationalised before sorting. 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 easily be sorted in a preferred order.



Step-by-Step Guide to Sorting Fractions in Perl

The following steps will guide us in writing our Perl code for sorting fractions.
Let's illustrate the steps to follow with the example case 5/9, 3/7, 1/2

Step 1:

Using the Find LCM in Perl class from the Primary Category, find the LCM of the denominators.
         ⇒ LCM of 9, 7 & 2 = 126

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.
         ⇒ ((5 x 14), (3 x 18), (1 x 63))/126
         = (70, 54, 63)/126

Step 3:

Go ahead and sort the numerators in our order of choice.
         ⇒ In ascending order:
         54/126, 63/126, 70/126
         = 3/7, 1/2, 5/9


Create a new Perl module file; call it SortFraction.pm.
Type out the adjoining Perl code for sorting fractions in ascending and descending orders.


Note: You can comment out the SubtractFraction Perl object code in the main class from the previous lesson or simply continue from where it stopped.


So! Perl Fun Practice Exercise - Sort 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 sorts those fractions.







Perl Code for Sorting Fractions - Module File

package SORTFRACTION;

BEGIN {
    require Exporter;

    # for the sake of standard
    our $VERSION = 2016.12;

    # Functions and variables to be exported by default
    our @EXPORT_OK = qw(sortAscending sortDescending);
}

use warnings;
use strict;
use parent "ADDFRACTION";

my $answer;
my (@numerators@denominators@new_numerators);
my (@final_numerators@final_denominators);
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 --
# reference to sorted numerators
# and denominators in that order
sub sortAscending {
    # STEPS 1, 2:
    my $call = ADDFRACTION->new(\%fractions);
    my @help = $call->canoniseFraction();
    @new_numerators = @{$help[0]};

    my @copy_numerators = @new_numerators;

    # STEP 3:
    # the little difference lies here
    @copy_numerators = sort {$a <=> $b@copy_numerators;

    # map sorted (transformed) fractions to the original ones
    for my $sorted (@copy_numerators) {
        # get index using array value
        my ($index) = grep { $new_numerators[$_] eq $sorted } 0 .. $#new_numerators;
        push @final_numerators$numerators[$index];
        push @final_denominators$denominators[$index];
    }
    
    return (\@final_numerators, \@final_denominators);
}

sub sortDescending {
    # STEPS 1, 2:
    my $call = ADDFRACTION->new(\%fractions);
    my @help = $call->canoniseFraction();
    @new_numerators = @{$help[0]};

    my @copy_numerators = @new_numerators;

    # STEP 3:
    # the little difference lies here
    @copy_numerators = sort {$b <=> $a@copy_numerators;

    # map sorted (transformed) fractions to the original ones
    for my $sorted (@copy_numerators) {
        # get index using array value
        my ($index) = grep { $new_numerators[$_] eq $sorted } 0 .. $#new_numerators;
        push @final_numerators$numerators[$index];
        push @final_denominators$denominators[$index];
    }
    
    return (\@final_numerators, \@final_denominators);
}


1;


Perl Code for Sorting Fractions - Main Class

#!/usr/bin/perl;
use strict;
use warnings;
use SORTFRACTION;

# Useful variables
my (@numerators@denominators@solutions);
my %fractions;

##
 # Sorting fractions
 ##
@numerators = (1359);
@denominators = (24210);
%fractions = (
    numerators     => \@numerators,
    denominators => \@denominators
);

print "\n    Sorting in ascending order the fractions:\n";
# Print as fraction
printf("%35s"" ");
printf("%9u", $_) for @numerators;
printf("\n%43s"" ");
print "- ,      " for 1 .. $#numerators;
printf("%2s""-");
printf("\n%35s"" ");
printf("%9d", $_) for @denominators;
print "\n";

# use the SortFraction class
my $sort_fract = SORTFRACTION->new(\%fractions);
@solutions = $sort_fract->sortAscending();

@numerators = @{$solutions[0]};
@denominators = @{$solutions[1]};

# Print as fraction
printf("\n%35s"" ");
printf("%9u", $_) for @numerators;
printf("\n%43s""Answer =    ");
print "- ,      " for 1 .. $#numerators;
printf("%2s""-");
printf("\n%35s"" ");
printf("%9u", $_) for @denominators;


print "\n\n";



<< Previous Next >>