The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w
# gen_primes - generate a perlish list of prime factors up to and
# including 2**32-1

# This software is written by Jonathan Feinberg, and is free for all
# copying, use, and tattooing.
use strict;

my @progs = qw! ./primes ./factor !;

my @primes = (2);
my @psquared = (4);
my $max = 2**32 - 1;
my $i;
FIND_NEXT_PRIME:
for (my $n = 3; ; $n += 2) {
 IS_IT_PRIME:
  for ($i = 0; $i < @primes; $i++) {
    next FIND_NEXT_PRIME if $n % $primes[$i] == 0;
    last IS_IT_PRIME if $psquared[$i] > $n;
  }
  push @primes, $n;
  push @psquared, $n**2;
  last FIND_NEXT_PRIME if $psquared[-1] > $max;
}

my $table = '';

my $t = '  ';
foreach my $p (@primes) {
  $t .= "$p, ";
  if (length $t > 70) {
    $table .= "$t\n";
    $t = '  ';
  }
}
$table .= "$t\n" if length $t > 2;

{
  local $^I = '~';
  local @ARGV = @progs;
  while (<>) {
    s/#PRIMES GO HERE/$table/;
    print;
  }
}

exit 0;