A problem with Math::Prime::XS



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?

.



Relevant Pages

  • Re: Regular expression: How to determine wether entry is a number?
    ... use strict; ... not letters. ... A regular ... my $num = 3; ...
    (perl.beginners)
  • Interesting behaviour with lexical variable
    ... use warnings; ... I found out that once the loop get executed for the second ... sub main { ... Variable "$num" will not stay shared at x.pl line 11. ...
    (comp.lang.perl.misc)
  • Re: Longest prefix match
    ... use warnings; ... #get rid of the sort and implement min and max ... my ($shortest, $longest) = ... print "$num does not have a rate\n"; ...
    (perl.beginners)
  • Re: Top 10 list algorithm
    ... Well I timed yours and wrote mine we have a draw timewise, ... sys 0.00 ... use strict; ... use warnings; ...
    (comp.lang.perl.misc)
  • Re: Help on Win32 API
    ... barewords as you have done. ... Maybe you should also turn on warnings? ... int wrap_OpenAduDeviceBySerialNumber(char * str, unsigned long num) { ... Rob ...
    (comp.lang.perl.modules)