[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [MacPerl] Prime Numbers





Here's another implementation of the trial division method of generating
primes. It's not real pretty (uses globals instead of parameters or call by
 name) but
it's fast.

$limit=1000;   # highest number to consider. primes larger than this will
not be generated.
@primes=(1);
for ($i=2;$i<$limit;++$i)
     {
     push @primes,$i if &isprime();
     }
$,=",\n";
print "prime list:\n";
print @primes;
exit;

sub isprime
     {
     $sr = sqrt($i);
     for ($j=0;;++$j)
          {
          next if $primes[$j] == 1;
           return 1 if $j > $#primes || $primes[$j] > $sr;
          return 0 if int($i/$primes[$j])==$i/$primes[$j];
          }
     }



===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org