[Shootout-list] nsieve Perl bench

Shoe Lace shoelace_822695@hotmail.com
Mon, 21 Mar 2005 16:13:58 +1100


This is a multi-part message in MIME format.

------=_NextPart_000_0008_01C52E30.FD079E50
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

Here is nseive in Perl.

Sorry if it doesn't attatch nicely I couldn't find the option.


------=_NextPart_000_0008_01C52E30.FD079E50
Content-Type: application/octet-stream;
	name="nseive.perl"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="nseive.perl"

#!/usr/bin/perl

# /*
# * The Great Computer Language Shootout
# * http://shootout.alioth.debian.org/
# *
# * Perl version of nseive benchmark
# * written David Pyke, March 2005
# */

#########################################################################=
###########
sub nseive($)
{
	my ($m) =3D @_;
	my @a =3D ();

#	print "m =3D $m\n";

	my $count =3D 0;
	for (my $i =3D 2; $i < $m; $i++){
		if (!defined $a[$i] ) {
			for ($j =3D $i + $i; $j < $m; $j +=3D $i){
				$a[$j] =3D 1;
			}
			$count++;
		}
	}
	return $count;
}

#########################################################################=
###########
sub nseive_test($)
{
	my($n) =3D @_;
#	print "n =3D $n\n";

	$m =3D 2**$n * 10000;

	my $ncount=3D nseive $m ;

	printf "Primes up to %8u %8u \n", $m, $ncount;
}
#########################################################################=
###########
#main
	my $N =3D ($ARGV[0] < 1) ? 1 : $ARGV[0];

	nseive_test $N ;

	nseive_test ($N-1)  if ($N >=3D1);
	nseive_test ($N-2)  if ($N >=3D2);
	exit;

#END OF FILE

------=_NextPart_000_0008_01C52E30.FD079E50--