#! 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 ];