Randomness of Prime Numbers | Maths Explanation for Perl Kids
Prime numbers are natural numbers greater than 1 with no positive divisors other than 1 and itself. Prime numbers, in ascending order, are:
                        Because prime numbers are so random in their progression,
                        they are very difficult to predict as they get larger.
                        For the reason of their unpredictability, prime number are applied in 
                        
- Cryptography: RSA encryption relies on large prime numbers.
 - Hashing: Prime numbers help reduce collisions in hash functions.
 - Data structures: Primes are used in sizing hash tables and optimizing algorithms.
 
In this beginner-friendly Maths Perl tutorial for kids, we'll show how to list prime numbers in a fun and interactive way for STEM students.
Writing a code in Perl to list prime numbers will involve checking every number in a range of interest and gathering those that are without factors.
Logic for Determining Prime Numbers | Detailed Explanation for Perl Kids
                        Say we want to implement a Perl algorithm to list all prime numbers between 2 and 100 inclusive,
                        we would start from 2; check to see whether it has any factors;
                        keep it as a prime number if it has no factors; otherwise discard it.
                        We would then go to 3, and repeat the same process.
                        Repeat same for 4, 5, 6, 7, ..., 98, 99, 100.
                        
                    
                        But we can always use a few tricks for the Perl algorithm...
                    
Step 1:
                        First, we'll start our range from 9 (keeping 2, 3, 5, and 7 as prime numbers).
                    
Step 2:
                        Next, we'll only check through odd numbers.
                    
Step 3:
Next, we'll check against the factors 2, 3, and 5.
Step 4:
                        Lastly, we'll check with a subset of smaller  
                        prime numbers that we'll gather as our check progresses.
                    
                        Create a new Perl class file; File, New File.
                        Call it listPrimeNumbers.pm.
                        
                        Type out the adjoining Perl code that lists prime numbers.
                    
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 Prime Numbers
As a fun practice exercise, feel free to try out your own boundary values, and see how the Perl code lists the prime numbers between those boundary values.
Perl Code for List Prime Numbers - 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(getPrimes);
}
use warnings;
use strict;
use POSIX qw(ceil pow); # round andlround seemed to be unavailable, so we go for 'ceil'
my ($start, $stop, $progress, $index, $square_root, $do_next_iteration);
my (@result, @list_of_primes);
# 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;
($start, $stop) = @_;
# STEP 1:
$progress = 9;
@list_of_primes = (2, 3, 5, 7);
$square_root = 0;
}
# Returns an array reference of the desired set of prime numbers
sub getPrimes {
# STEP 2:
for (; $progress <= $stop; $progress += 2) {
$do_next_iteration = 0; # a flag
# STEPS 3, 4:
# Check through already accumulated prime numbers
# to see if any is a factor of "progress".
$square_root = ceil(sqrt($progress));
$index = 0;
for (; $list_of_primes[$index] <= $square_root; $index++) {
if ($progress % $list_of_primes[$index] == 0) {
$do_next_iteration = 1;
last;
}
}
next if $do_next_iteration;
# if all checks are scaled,then "progress" is our guy.
push @list_of_primes, $progress;
}
return \@list_of_primes;
}
1;
Perl Code for List Prime Numbers - Main Class.
use strict;
use warnings;
use PRIMENUMBERS;
# Useful variables
my ($lower_boundary, $upper_boundary, $answer);
# Use the prime numbers module/class
$lower_boundary = 3;
$upper_boundary = 7;
my $prime_list = PRIMENUMBERS->new($lower_boundary, $upper_boundary);
$answer = $prime_list->getPrimes();
print "Prime numbers between $lower_boundary and $upper_boundary are:\n@{$answer}\n";
print "\n\n";