Re: listing prime numbers in a range (Beginning Perl exercise)

From: James Edward Gray II (james_at_grayproductions.net)
Date: 03/03/04


Date: Wed, 3 Mar 2004 10:14:56 -0600
To: Stuart White <poovite@yahoo.com>

On Mar 2, 2004, at 9:10 PM, Stuart White wrote:

>
>>
>> Stuart, can we please see the whole script?

Stuart, I reworked the for loops to do what I believe you intended and
made a few other general cleanups. See if this gets you going:

#!/usr/bin/perl
# primeNumbers.pl
use warnings;
use strict;

print "Enter a number that is bigger than 2:\t";
chomp( my $input = <STDIN> );
print "This is your number: $input \n";
my @list = (2 .. $input);

#this foreach loop goes through the @list array, putting the odd
#numbers into a new array, @primelist
my @primelist;
foreach (@list) { unshift @primelist, $_ if ($_ % 2); }

#for loops that will loop through @list and @primelist, getting the
#modulus of @primelist. I anticipate that this will isolate prime
#numbers.
my @newPrimeList;
PRIMES: for my $possible_prime (@primelist) {
        for my $test (3..($possible_prime - 1)) {
                next PRIMES unless $possible_prime % $test;
        }
        push @newPrimeList, $possible_prime;
}
  print "this is primelist: @primelist\n";
  print "this is newPrimeList: @newPrimeList\n";

__END__

James