What are Factors? | Maths Explanation for Perl Kids
                        Factors are the complete set of integers that will divide 
                        a particular number without remainder.
                        Take 36 as an example, it's complete set are:
                    
                        Other than prime numbers, every other number has at least 
                        one factor - not considering 1 as factor.
                        Where there is just one factor, then this factor is the square 
                        root of the number in question; 
                        In this guide, we'll explore the math behind factors-of-numbers and walk through 
                        how to code a Perl algorithm for listing factors in a simple and fun way.
                    
Code Logic for Factorising Numbers in Perl - Fun Maths Exercise
                        Actually, we've been doing factors over the last two demonstrations (lessons).
                    
                        We can implement a Perl algorithm for factorising a number by simply checking for our factors using the 
                        square-root of number range.
                        We'll start from 1 (which is always a factor).
                        For each found factor, we'll get the corresponding complementary factor
                        by dividing the number (whose factors we are trying to find),
                        by the found factor. 
                        
                        This Math activity and Perl script help primary school students understand factorization by listing all factors of a number.
                    
                        Create a new Perl module file; File, New File.
                        Call it listFactors.pm.
                        
                        Type out the adjoining Perl code for listing factors.
                    
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 - List Factors
As a fun practice exercise, feel free to try out your own different numbers, and see how the Perl code lists the factors of those numbers.
Perl Code for List Factors - 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(findFactors);
}
use warnings;
use strict;
use POSIX qw(ceil);
my ($find_my_factors, $sqrt_range);
my @found_factors;
# simulate an object construct
# takes two arguments -- besides its name;
# start and stop values for the range
sub new {
my $this = shift;
my $parameters = {@_};
bless $parameters, $this;
$this->_init(@_);
return $this;
}
# Simulate a constructor
sub _init {
my $self = shift;
$find_my_factors = shift;
@found_factors = (1, $find_my_factors);
$sqrt_range = ceil(sqrt($find_my_factors));
}
# Returns an array reference of the desired factors
sub findFactors() {
# Loop through 1 to 'find_my_factors' and test for divisibility.
for (2 .. $sqrt_range) {
if (($find_my_factors % $_) == 0) {
push @found_factors, $_;
# Get the complementing factor by dividing 'find_my_factor' by variable i.
push(@found_factors, ($find_my_factors / $_));
}
}
# Sort the array in ascending order; Not entirely necessary.
@found_factors = sort {$a <=> $b} @found_factors;
return \@found_factors;
}
1;
Perl Code for List Factors - Main Class.
use strict;
use warnings;
use LISTFACTORS;
# Useful variables
my ($test_guy, $answer);
# Use the list factors module/class
$test_guy = 48;
my $factor_list = LISTFACTORS->new($test_guy);
$answer = $factor_list->findFactors();
print ("Factors of $test_guy include:\n", join(", ", @{$answer}), "\n");
print "\n\n";