Stuart White wrote: > So I figured out from folks on the list, Beginning > Perl, and some print statements that > @list = (2 .. $input); > > puts the range of numbers into an array, and that if I > stringified it, then it would put the entire range > into $list[0] > > Now that I've figured that bit out, I went on to > remove all the even numbers from the list. I got that > done with this: > foreach (@list) > { > if ($_ % 2 != 0) > { > unshift(@primelist, $_); > @primelist\n"; > } > } > > Now what I'd like to do to test whether or not I have > a prime number is to get at each element in > @primelist, and use the modulus operator on each > element against each element in @list. For example: > $primelist[0] % $list[0] > $primelist[0] % $list[1] > $primelist[0] % $list[2] > $primelist[1] % $list[0] > $primelist[1] % $list[1] > $primelist[1] % $list[2] > $primelist[2] % $list[0] > $primelist[2] % $list[1] > $primelist[2] % $list[2] > > and if the result doesn't equal 0 for every test > except for against itself, then I want to unshift it > onto @primelist. > > I thought I'd do this with nested foreach loops, but > that didn't seem to work. Any ideas? > Thanks. > > oops, here's my code: > > #!/usr/bin/perl > # primeNumbers.pl > use warnings; > use strict; > my $input; > my @list; > my @primelist; > print "I want you to enter a list of numbers bigger > than 2.\n"; > print "To mark the end of your list, enter 0.\n"; > print "Enter a number that is bigger than 2:\t"; > $input = <STDIN>; > chomp($input); > print "This is your number: $input \n"; > @list = (2 .. $input); > > print "this is this the fourth element, but has an > index of 3: $list[3]\n"; > foreach (@list) > { > if ($_ % 2 != 0) > { > unshift(@primelist, $_); > print "this is the inner loop of primelist: > @primelist\n"; > } > } > print "this is the outer loop of primelist: > @primelist\n";
You must b getting bored and tense with all that code and numbers and stuff. Take a break, and enjoy some rolling screen graphics: my @primes = (2); my $candidate = 3; while(1) { my $limit = $candidate ** 0.5; my $has_factor = 0; foreach (@primes) { if ($_ > $limit) { push @primes, $candidate; last; } unless ($candidate % $_) { $has_factor = 1; last; } } unless ($has_factor) { print "$_ " foreach @primes; print "\n"; } $candidate++; } Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>