C:\test>primes.pl 15e6
The 15000000th prime is: 275604541
1 trial of Finding the 15000000 prime (47.118ms total)
1 trial of Retrieving the first 15000000 primes (11.906s total)
####
#! perl -slw
use strict;
package primes;
open my $fh, '<:raw', 'data\primes.deltas.bin' or die $!;
read( $fh, my( $deltas ), -s( 'data\primes.deltas.bin' ) );
close $fh;
sub firstNprimes {
my( $n ) = @_;
my @primes = unpack 'C*', $deltas;
$primes[ $_ ] += $primes[ $_ - 1 ] for 1 .. $#primes;
return \@primes;
}
sub primeN { return unpack "%32C$_[0]", $deltas; }
package main;
use Benchmark::Timer;
die 'Supply N' unless @ARGV;
$ARGV[ 0 ] +=0; ## force numeric
my $T = new Benchmark::Timer;
$T->start( "Finding the $ARGV[ 0 ] prime" );
print "The $ARGV[ 0 ]th prime is: ", primes::primeN( $ARGV[ 0 ] );
$T->stop( "Finding the $ARGV[ 0 ] prime" );
$T->start( "Retrieving the first $ARGV[ 0 ] primes" );
my $primes = primes::firstNprimes( $ARGV[0] );
$T->stop( "Retrieving the first $ARGV[ 0 ] primes" );
$T->report;
print "The first 100 primes are:";
print for @{ $primes }[ 0 .. 99 ];
print "The last 100 primes (of the first ARGV[ 0 ]) are:";
print for @{ $primes }[ -100 .. -1 ];
##
##
$checksum = unpack '%32C*', $bytes;
##
##
sub primeN { return unpack "%32C" . (0+$_[0]), $deltas; }