The Intrique of Prime Numbers | Detailed Explanation for Perl Kids
Prime numbers are tricky to spot.
A number that looks like a prime may in fact be a multiple
of a smaller prime number.
In this math programming tutorial for kids, we explore how Perl can be used to verify prime numbers efficiently.
So let's see how to know for sure that a number is a prime.
Of-course there is only one way:
Understanding Prime Number Logic in Perl
A prime number is one that can only be divided by itself and one (1).
Let's try to draft a Perl algorithm that checks prime numbers, with the number 97 in consideration.
To know for sure whether it is a prime number, we will
recursively (repetitively) divide it by every number between
2 and 96 (97 minus 1).
If none of these divisors gives a remainder of zero, then 97
is certainly a prime number.
Create a new Perl module file; File, New File.
Call it CheckPrime.pm
Type out the adjoining Perl code for checking for primeness.
Base Theory of Quick-Check for Primeness in Perl
Since the world is always in a hurry, we can make use of a
little extra speed.
This Perl code example shows how to check if a number is prime using a fast algorithm based on complementary factors.
Consider the number 36; Its factors are:
1, 2, 3, 4, 6, 9, 12, 18 and 36.
Every factor of 36, when arranged in ascending or descending
order, can be divided into 2 equal parts at the position of its square-root.
1, 2, 3, 4, |, 9, 12, 18, 36
It is easily seen that every factor of 36 on one side of the
divide has a complementary factor on the other side.
Figure: Complementary factors to expedient quick check for prime numbers in Perl.
Hence, we can search for only a particular group of
factors, (preferably the more compact group, i.e, between
1 and √36) to see if 36 has any factors.
A fast Check for Primeness in Perl
So for our quick prime number check Perl algorithm, we will use the range of
2 to √number.
Type out the adjoining Perl code for fast prime number check. .
Note: You can simply tweak the existing function from the previous exercise.
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 - Check Prime Number
As a fun practice exercise, feel free to try out your own numbers,
and see how the Perl code checks the numbers to ascertain which ones are prime numbers.
Perl Code for Checking Prime - Module File.
package CHECKPRIME;
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(verifyPrime);
}
use warnings; use strict; use POSIX qw(ceil);
my$prime_suspect; our$a_factor;
# simulate an object construct # takes one argument -- besides its name; # value for the test sub new { no warnings; my$this = shift; my$parameters = {@_}; bless$parameters, $this; $this->_init(@_); return$this;
}
# Simulate a constructor sub _init { my$self = shift; $prime_suspect = shift;
}
# returns true if $prime_suspect is a prime; false otherwise. sub verifyPrime { # prime_suspect is a prime number until proven otherwise # Loop through searching for factors. for (2 .. $prime_suspect) { if (($prime_suspect % $_) == 0) { $a_factor = $_; return0;
}
} # If no factor is found: return1;
}
1;
Perl Code for Checking Prime - Main Class.
#!/usr/bin/perl; use strict; use warnings; use CHECKPRIME;
# Useful variables my ($test_guy, $result);
# Use the check prime module/class $test_guy = 98; my$prime_check = CHECKPRIME->new($test_guy); $result = "Prime State:\n"; if ($prime_check->verifyPrime()) { $result = "$test_guy is a prime number.";
} else { $result .= "$test_guy is not a prime number.\n"; $result .= "At least one factor of $test_guy is " . $CHECKPRIME::a_factor;
} print"$result\n";
print"\n\n";
Perl Code for CheckPrimeFast.pm
package CHECKPRIMEFAST;
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(verifyPrimeFast);
}
use warnings; use strict; use POSIX qw(ceil);
my$prime_suspect; our$a_factor;
# simulate an object construct # takes one argument -- besides its name; # value for the test sub new { no warnings; my$this = shift; my$parameters = {@_}; bless$parameters, $this; $this->_init(@_); return$this;
}
# Simulate a constructor sub _init { my$self = shift; $prime_suspect = shift;
}
# returns true if $prime_suspect is a prime; false otherwise. sub verifyPrimeFast { # prime_suspect is a prime number until proven otherwise # Loop through searching for factors. my$test_range = ceil(sqrt($prime_suspect)); for (2 .. $test_range) { if (($prime_suspect % $_) == 0) { $a_factor = $_; return0;
}
} # If no factor is found: return1;
}
1;
Perl Code for Checking Prime Fast - Main Class.
#!/usr/bin/perl; use strict; use warnings; use CHECKPRIMEFAST;
# Useful variables my ($test_guy, $result);
# Use the check prime module/class $test_guy = 49; my$prime_check = CHECKPRIMEFAST->new($test_guy); $result = "Prime State:\n"; if ($prime_check->verifyPrimeFast()) { $result = "$test_guy is a prime number.";
} else { $result .= "$test_guy is not a prime number.\n"; $result .= "At least one factor of $test_guy is " . $CHECKPRIMEFAST::a_factor;
} print"$result\n";