[Shootout-list] perl implementations of fannkuch, nsieve, takfp

Sean O'Rourke sorourke at cs.ucsd.edu
Sun Apr 23 22:54:51 UTC 2006


Here are Perl implementations of 3 of the new benchmarks.  I'm not
sure if the coding style is correct, but they produce the sample
answers.

/s

-------------- next part --------------
#!/usr/bin/env perl

sub tak {
    my ($x, $y, $z) = @_;
    $y < $x
        ? tak(tak($x-1.0, $y, $z),tak($y-1.0, $z, $x),tak($z-1.0, $x, $y))
        : $z;
}

$n = shift;
print tak(3.0*$n, 2.0*$n, 1.0*$n), "\n";
-------------- next part --------------
#!/usr/bin/env perl

sub sieve {
    my $n = shift;
    my $v = "\0" x (($n+1)/8);
    my $r = 0;
    for my $i (2..$n) {
        unless (vec $v, $i, 1) {
            ++$r;
            for (my $j = $i; $j < $n; $j += $i) {
                vec($v, $j, 1) = 1;
            }
        }
    }
    $r-1;
}

my $n = shift || 2;

for (0..2) {
    my $n = 10000 * 2 ** ($n - $_);
    printf "Primes up to %8d %8d\n", $n, sieve $n;
}
-------------- next part --------------
#!/usr/bin/env perl

sub permsize {
    my $n = 0;
    my @x = @things;
    while ($x[0] != 1) {
        @x[0 .. $x[0]-1] = reverse @x[0 .. $x[0]-1];
        ++$n;
    }
    return $n;
}

sub for_perms {
    my $n = shift;
    local @things = 1..$n;
    local $max=0;
    local *helper = sub {
        my $lo = shift;
        if ($lo < $#things) {
            for ($lo .. $#things) {
                @things[$lo,$_] = @things[$_,$lo];
                helper($lo+1);
                @things[$lo,$_] = @things[$_,$lo];
            }
        } else {
            my $tmp = permsize;
            $max = $tmp if $tmp > $max;
        }
    };
    helper(0);
    $max;
}

print for_perms(shift), "\n";


More information about the Shootout-list mailing list