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







<< PreviousNext >>

Finding HCF and GCD in Perl - Kids Fun Exercise



What is HCF and GCD? | Maths Explanation for Perl Kids

Highest Common Factor or Greatest Common Divisor is the highest-valued-factor or greatest-valued-divisor common to a set of numbers.

In this Perl math project, we'll explore how to find the Highest Common Factor (HCF) and Greatest Common Divisor (GCD) of numbers. This tutorial is perfect for primary school students learning to code with Perl.

A common method for finding H.C.F. - G.C.D. is repeated factorization using only common factors.

If we have the set of numbers 30, 48 and 54 for example, their H.C.F. or G.C.D. is found thus:

Steps for calculating HCF using factorization method in Perl
Figure: Math steps for calculating HCF using factorization method in Perl.

Hence, H.C.F. of 30, 48 and 54 = 2 X 3 = 6


How to find HCF of multiple numbers in Perl | Step-by-step Guide.

We shall follow the steps below in our Perl algorithm for finding HCF.

Step 1:

Do a numerical sort on the set so its first member is the smallest in the set.

Step 2:

Starting with 2, iteratively check through the set of numbers for a common factor.

Step 3:

For each common factor, divide every member of the number set by the common factor.

Step 4:

Repeat from step 2 recursively until there are no more common factors.

Create a new Perl module file; File, New File.
Call it FindHCF.pm
Type out the adjoining Perl code for finding Highest Common Factor (H.C.F.) or Greatest Common Divisor.


Note: You can comment out the Perl code for the main class from the previous lesson if you have been following.


So! Perl Fun Practice Exercise - Find HCF

As a fun practice exercise, feel free to try out your own numbers, and see how the Perl code finds the HCF of those numbers.







Perl Code for Find HCF - Module File.

package HCF;

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(getHCF);
}

use warnings;
use strict;

my ($index$all_round_factor$calc_result);
my (@set_of_numbers@common_factors);

# simulate an object construct
# takes one argument  -- besides its package name;
# reference to set whose HCF is to found
sub new {
    no warnings;

    my $this = shift;
    my $parameters = {@_};
    bless $parameters$this;
    $this->_init(@_);
    return $this;
}

# Simulate a constructor
sub _init {
    my $self = shift;
    
    my $tmp = shift;
    @common_factors = ();
    @set_of_numbers = @{$tmp};
    # STEP 1:
    @set_of_numbers = sort {$a <=> $b@set_of_numbers;

    $index = 2;
    $all_round_factor = 0# boolean state flag
    $calc_result = 1;
}

# does the grunt work
# takes no arguments but requires '@set_of_numbers' to be set
sub findHCFFactors {
    while ($index <= $set_of_numbers[0]) {

        # Check for factors common to every member of 'set_of_numbers'
        $all_round_factor = 1;
        # STEP 2:
        for (0 .. $#set_of_numbers) {
            $all_round_factor = 0 if !($all_round_factor && $set_of_numbers[$_] % $index == 0);
        }
        # STEP 3:
        # Divide every member of 'set_of_numbers by each common factor
        if ($all_round_factor) {
            $set_of_numbers[$_] /= $index for 0 .. $#set_of_numbers;
            push @common_factors$index;
            # STEP 4:
            return findHCFFactors();
        }
        $index++;
    }
    return;
}

# Returns a scalar value of the HCF
sub getHCF {
    findHCFFactors();

    #iterate through and retrieve members
    for (@common_factors) {
        $calc_result *= $_;
    }
    
    return $calc_result;
}


1;


Perl Code for Find HCF - Main Class.

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

# Useful variables
my $answer;
my @set;

# Use the HCF module/class
@set = (203040);
my $hcf = HCF->new(\@set);
$answer = $hcf->getHCF();
print ("The H.C.F. of "join(", "@set), " is $answer\n");


print "\n\n";



<< PreviousNext >>