in reply to Prime Number Finder
Note: the grep is fairly costly. Find a better algorithm if you're looking for all primes up to, say, 100,000.use strict; sub primes { my @n = (2..shift); my @p; while (@n && (push @p, shift @n)) { @n = grep { $_ % $p[-1] } @n; } return @p } # find all primes through 100 print join ',',primes(100);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Prime Number Finder
by Anonymous Monk on Nov 03, 2003 at 05:34 UTC |