#!/usr/bin/perl -s
#Assignment #1 Written by Thomas Los
#Finding Prime Number
#system("clear");
$min=100;
$max=100;
while ($min != 0 and $max != 0)
{
print "\n\n";
print "What is floor of the range of numbers you want??\n\n";
$min=<STDIN>;
chomp($min);
print "What is the ceiling of the range of numbers you want???\n\n";
$max=<STDIN>;
chomp($max);
if (($min != 0) and ($max != 0))
{
if ($min <0 || $max<0 || $min>$max) {
print "You are retarded!!\n";
print "Run the program again and try again! Your Min can't be greater than the maximum
Your number can't be negative either!!\n";
exit;
}
$ret=fork();
if ($ret == 0)
{
&prime($min,$max);
}
}
}
$prime=0;
sub prime($min,$max) {
$beginning = (times)[0];
# Square root for speed.
$sqrt_max=sqrt($max);
# Teh range
for ($i=$min;$i<=$max;$i++) {
# Assume it's a prime.
$is_prime=1;
# Skip 1, or the inner if will go wrong
for ($j=2;$j<=$sqrt_max;$j++) {
$y=$i%$j;
# We only need $y = 0 and not to be the same number.
# 2 is the only exception.
if (($y==0 && $i!=$j) || $i==2) {
# Conditions met, it's not a prime.
$is_prime=0;
last;
}
}
if ($is_prime) {
$prime++;
}
}
$end = (times)[0];
$time_elapsed = $end - $beginning;
print "\n\nThe child process id is $$\n";
print "\nThe given range was from $min to $max\n";
print "\nThe amount of prime numbers is $prime.\n";
print "\nIt took $time_elapsed to complete your request\n";
}