Skip to content

anonymous /mersenne.pl

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/bin/perl
use warnings;
use strict;
use bignum;
$|=1;
my $p = $ARGV[0];
die "Usage: $0 number\n" if !$p;
my $number = ( 2**$p ) - 1;
printf "Testing 2^%s - 1 == %s\n", $p, $number;
my $divisor = find_divisor($number);
printf "2^%s - 1 is %s.\n", $p, ($divisor ? "divisible by $divisor" : 'prime');
exit;
sub find_divisor {
my $number = shift;
my $limit = sqrt($number);
my $divisor = 3;
my $passes = 0;
my $result;
while ($divisor <= $limit) {
$passes +=1;
if ($number % $divisor == 0) {
$result = $divisor;
last;
}
$divisor +=2;
print "\x0d".$passes if $passes % 1000 == 0;
}
print "\x0d".$passes." passes\n";
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.