Just for the sake of it, and I apologise profusely for the top posting.. Here is a summary of the posted prime calculators for testing your results against.
>(1) Ohad #------------------------------------------------------- perl -e 'print"@{[grep{(1x$_)!~/^(11+?)\1+$/}2..shift||1e2]}\n"' <number> #------------------------------------------------------- >(2) Joseph #------------------------------------------------------- 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++; } #------------------------------------------------------- >(3) me (Hey, it's a Mee Too!) #------------------------------------------------------- $max=shift||17393; %notprime=(); @prime=(); for $n (2..$max){ unless($notprime{$n}){ push @prime,$n; $notprime{$_*$n}=1 for 2..$max/$n; } } print "Found $#prime prime numbers..".$/; print "@prime\n"; #------------------------------------------------------- Brief explanation.. (1) Ohad posted an obfuscated calculator which runs the array (2..<number>) through grep(). The pattern in grep is '(1 x $_) !~ /^(11+?)\1+$/' which is a tricky way of getting perls enormously powerful regex exgine to determine if there is a way of dividing a string of length int($_) characters into a number of same length substrings. Probably memory hungry, but definitely cpu hungry for the regexp. Note that for any value '$x', "(1 x $x) !~ /^(11=?)\1+$/" will return true if the number is prime. Excellent way to perform a (quick?) once off test against a known number without calculating every other prime. It uses the obscure perl @{[ expr ]} to magically interpolate code within quotes. (2) Joseph posted a calculator which uses the feature of prime numbers that the largest number you need to divide it by is the square root of itself ($_**0.5) and that you only need to test if the number you are examining is divisible (ie mod==0) by another prime you have already found. Hence, very quick and very cpu/memory cheap. Why not test against every number? If you test against a non-prime number, you are simply testing against a multiple of a prime of course. (3) A very simple implementation of an erogenous sieve (iud? did I say that wrong?) which keeps all primes in '@primes' and all 'eliminated' entries in '%notprime'. quick, but likely more memory intensive than (2). Cheers. David -- Senior Technical Specialist I d e n t i t y S o l u t i o n s Level 1, 369 Camberwell Road, Melbourne, Vic 3124 Ph 03 9813 1388 Fax 03 9813 1688 Mobile 0417 595 550 Email [EMAIL PROTECTED] > -----Original Message----- > From: Stuart White [mailto:[EMAIL PROTECTED] > Sent: Tuesday, 2 March 2004 11:14 AM > To: James Edward Gray II > Cc: [EMAIL PROTECTED] > Subject: Re: listing prime numbers in a range (Beginning Perl > exercise) > > > > That's not really accurate. What was said to that: > > > > @list = "@list"; > > > > Creates a joined string of elements and replaces the > > list with that one > > item. It's perfectly reasonable to stringify an > > array, without > > altering the array's contents: > > > > print "@list\n"; > > > > I'm not sure I understand. > > > > > That can just be if ($_ % 2), if you prefer. Zero > > That makes perfect sense. Thanks. > > > > { > > > unshift(@primelist, $_); > > > @primelist\n"; > > > > The above line is not a valid Perl statement. > > > > Oh, the "@primelist\n"; ? I'm not sure how that got > there... > > > > 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? > > > > Define "didn't seem to work"? On second thought, > > just post that nested > > foreach and let us help you fix it. > > > > James > > > > Ok, I went with for loops. As above, I want to get > the modulus of numbers in one array by comparing the > numbers of another array. It's not doing what I > expect it to. I'm not sure if the first loop is > really running either. What am I doing wrong? (Does > that question make sense?) > > Also, I'm getting an error: Use of uninitialized value > in modulus (%) at primeNumbers.pl line 40, <STDIN> > line 1. > Illegal modulus zero at primeNumbers.pl line 40, > <STDIN> line 1. > What do those errors mean exactly? Thanks. > > > __________________________________ > Do you Yahoo!? > Get better spam protection with Yahoo! Mail. > http://antispam.yahoo.com/tools > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>