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







<< PreviousNext >>

Perl Code to List Prime Pactors of a Number - Math Project for Primary Students



What are Prime Factors? | Maths Explanation for Perl Kids

Finding prime factors is all about selecting those factors of a number that are prime.
The prime factors of 36 as an example, are:

2 X 2 X 3 X 3.

In this guide, we'll thoroughly explain prime factorisation and show how to code a Perl algorithm to find prime-factors in a detailed and interactive manner.
This Math exercise and Perl algorithm will help young students understand prime factorization by listing all prime factors of a number.


Step-by-step Guide to Prime Factorisation of Numbers in Perl

We'll go about the Perl algorithm to find prime factors in a simple way:

Step 1:

Starting with 2, find the first factor of the number of interest - this factor will definitely be a prime.
Store this factor away.

Step 2:

Divide number in question by found factor.

Step 3:

Using result from Step 2 as the new number in question (number whose prime factors we are trying to find), repeat Step 1.

Step 4:

Continue recursively until we arrive at 1.

Step 5:

We'll use the square-root of number range.


Create a new Perl module file; File, New File.
Call it listPrimeFactors.pm.

Type out the adjoining Perl code for listing prime factors.


All those steps in a few lines of Perl code;
Now Perl functions come in handy, don't they?

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


So! Perl Fun Practice Exercise - List Prime Factors

As a fun practice exercise, feel free to try out your own different numbers, and see how the Perl code lists the prime factors of those numbers.







Perl Code for List Prime Factors - Module File.

package LISTPRIMEFACTORS;

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

use warnings;
use strict;
use POSIX qw(ceil);

my ($find_my_factors$i);
my @found_prime_factors;

# simulate an object construct
# takes one argument  -- besides its name;
# value whose prime factors is to be 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;
    
    $find_my_factors = shift;
    @found_prime_factors = ();
    # STEP 1:
    $i = 2;
}

# takes one argument, the candidate for factorisation;
# returns an array reference of the prime factors
sub onlyPrimeFactors {
    my $in_question = $find_my_factors;
    $in_question = shift unless $_[0] eq __PACKAGE__;
    my $temp_limit;
    $temp_limit = ceil(sqrt($in_question));

    while ($i <= $temp_limit) {
        # STEP 4:
        # avoid an infinite loop with the i != 1 check.
        if ($i != 1 && ($in_question % $i) == 0) {
            push @found_prime_factors$i;
            # STEPS 2, 3:
            return onlyPrimeFactors($in_question / $i);
        }
        $i++;
    }
    push @found_prime_factors$in_question;

    return \@found_prime_factors;;
}


1;

 

Perl Code for List Prime Factors - Main Class.

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

# Useful variables
my ($test_guy$answer);

# Use the list prime factors module/class
$test_guy = 97;
my $prime_factors = LISTPRIMEFACTORS->new($test_guy);
$answer = $prime_factors->onlyPrimeFactors();
print ("Prime factorising $test_guy gives:\n"join(" X ", @{$answer}), "\n");


print "\n\n";




<< PreviousNext >>