anonymous / mersenne.pl
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View mersenne.pl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/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.