A problem with Math::Prime::XS
- From: ofer@xxxxxxxxxx
- Date: 14 Feb 2006 09:35:15 -0800
I don't know what exactly the problem is with this module.
But watch this:
1st program:
---------------------------------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Math::Prime::XS qw( is_prime );
die( 'please specify a number' ) unless ( @ARGV == 1 );
my $num = $ARGV[0];
if ( is_prime( $num ) ) {
print "$num is prime.\n";
} else {
print "$num is not prime.\n";
}
---------------------------------------------------------
2nd program:
---------------------------------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use bigint;
die( 'please specify a number' ) unless ( @ARGV == 1 );
my $num = $ARGV[0];
if ( is_prime( $num ) ) {
print "$num is prime.\n";
} else {
print "$num is not prime.\n";
}
sub is_prime {
my ( $num ) = @_;
if ( ( ( $num % 2 ) == 0 ) || ( $num <= 3 ) || ( $num !~ /^\d+$/ )
) {
return 0;
}
for ( my $check_num = 3; $check_num <= int( sqrt( $num ) );
$check_num+=2 ) {
if ( ( $check_num % 5 ) == 0 ) {
next;
} elsif ( ( $num % $check_num ) == 0 ) {
return 0;
}
}
return 1;
}
---------------------------------------------------------
I know that the script I wrote is not very good or very smart. But take
a look in the results:
ofer@nettux:~/scripts/prime_numbers$ time ./prime_using_xs.pl 123456
123456 is not prime.
real 0m9.196s
user 0m8.837s
sys 0m0.008s
ofer@nettux:~/scripts/prime_numbers$ time ./my_prime_check.pl 123456
123456 is not prime.
real 0m0.064s
user 0m0.052s
sys 0m0.012s
ofer@nettux:~/scripts/prime_numbers$ time ./prime_using_xs.pl 1234567
Segmentation fault
real 0m0.015s
user 0m0.012s
sys 0m0.004s
ofer@nettux:~/scripts/prime_numbers$ ./my_prime_check.pl 1234567
1234567 is not prime.
Anybody knows what is wrong with Math::Prime::XS?
.
- Follow-Ups:
- Re: A problem with Math::Prime::XS
- From: Tassilo v. Parseval
- Re: A problem with Math::Prime::XS
- Prev by Date: Re: Loading Perl Modules from same directory as script
- Next by Date: Re: Loading Perl Modules from same directory as script
- Previous by thread: debugging (stepping thru in debugger) within a try block
- Next by thread: Re: A problem with Math::Prime::XS
- Index(es):
Relevant Pages
|
|