Created
September 12, 2011 22:48
-
-
Save fibo/1212698 to your computer and use it in GitHub Desktop.
Crivello Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use Math::Prime::XS ':all'; | |
my $l = $ARGV[0]; | |
my @primes = primes($l); | |
my $p1 = $primes[-2]; | |
my $p2 = pop @primes; | |
my $n = $p1 * $p1; | |
my @r; | |
for my $p (@primes) { | |
my $r = $n % $p; | |
push @r, $r; | |
} | |
while ( $n < $p2 * $p2 - 1 ) { | |
$n++; | |
my $is_prime = 1; | |
for ( my $i = 0 ; $i <= $#r ; $i++ ) { | |
$r[$i] = ( $r[$i] + 1 ) % $primes[$i]; | |
} | |
for my $r ( @r ){ | |
last unless $is_prime; | |
if( $r == 0 ){ | |
$is_prime = 0; | |
} | |
} | |
print "n = $n:\t"; | |
#print "$_," for @r; | |
# se scommento questa riga confronto is_prime col mio crivello e | |
# mettendo e togliendo il commento si nota la differenza di velocità | |
# se si parte ad esempio da 10000 | |
# print "Math::Prime " if is_prime($n); | |
print "OK" if $is_prime; | |
print "\n"; | |
#<STDIN>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment