...with the actual calculation taking one single grep statement:
That's rather misleading. There's a nested map as well, but you didn't count that.

I've played with this idea quite a bit in the past, and never cared much for over-complexifying it. I'll post 2 ideas here, one from Abigail from CLPM, and one I dreamed up for my keystroke programmable TI-95 longer ago than I care to imagine.

From Usenet, June '97:

#!/usr/local/bin/perl -wl use strict; my ($prime, $max) = (2, shift || 50); # First prime, max numb +er. my @sieve = (0, 0, map {1;} ($prime .. $max)); # Init sieve. while ((my $product = $prime * $prime) <= $max) { do {$sieve [$product] = 0;} while (($product += $prime) <= $max); do {$prime ++;} while !$sieve [$prime]; } map {print if $sieve [$_];} (0 .. $max);
I like this because it's easy to understand, and reasonably efficient. For instance, once the next $prime is found, all unmarked candidates less than the $prime**2 are also prime -- no need for trial division on them.

Notice that this takes up some memory. On the TI-95, there was only a little memory, so computing the first million primes would be painful. And keeping a list of anything besides primes was even more limiting. With that in mind, here's another way:

#!/your/perl/here use strict; use warnings; use integer; $|=1; my $stop = (shift or 50); print "2"; my @primes; TRIAL_PRIME: for (my $i=3;$i<$stop;$i+=2) { for my $p (@primes) { next TRIAL_PRIME unless ( $i % $p ); } push @primes, $i; print ", $i"; } print "\n"; exit;
That's reasonably fast with a small memory footprint. For a slight speedup, and less memory, use a bit vector.

Update: Can't spell Abigail (at least the link was correct?)

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: Answer: Easy way to list prime numbers 1 to 100 by QM
in thread Easy way to list prime numbers 1 to 100 by l.frankline

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.