Click here to get back home

Math::Pari 'factor' is wrong

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Math::Pari 'factor' is wrong gamo 03-17-2006
Posted by gamo on March 17, 2006, 1:41 pm
Please log in for more thread options



#!/usr/local/bin/perl -w

# use Math::PariInit qw( primes=12000000 stack=1e8 );
# use Math::Pari qw(:all);
use Math::Pari 'factor';

# $i = PARI 0;
# $P = PARI 1;

for $i (0..1_000_000_000) {
$P = 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;
$f = factor($P,0);
# print "$f\n";
# next if ($f =~ /-1/);
@fact= ($f =~ /[\[|\;](\d+)\,/g);
# print "@fact\n";
for $j (@fact){
        if ($j%10!=1){
         print "$f -> $j factor of P($i)=$P\n";
        }
}
# last if $i==1000;
}


__END__


This code gives incorrects results as -1 as factor of a positive
integer or 5 as factor of a number not terminated in 5 or 0.

I do a sample calculation using 'echo "factor(number)" | gp' and this
gives correct results.

TIA, best regards

Posted by gamo on March 17, 2006, 8:53 pm
Please log in for more thread options


This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1598280312-1142646809=:27081
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Fri, 17 Mar 2006, gamo wrote:

>=20
> #!/usr/local/bin/perl -w
>=20
> # use Math::PariInit qw( primes=3D12000000 stack=3D1e8 );
> # use Math::Pari qw(:all);
> use Math::Pari 'factor';
>=20
> # $i =3D PARI 0;
> # $P =3D PARI 1;
>=20
> for $i (0..1_000_000_000) {
> $P =3D 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;
> $f =3D factor($P,0);
> # print "$f\n";
> # next if ($f =3D~ /-1/);
> @fact=3D ($f =3D~ /[\[|\;](\d+)\,/g);
> # print "@fact\n";
> for $j (@fact){
> =09if ($j%10!=3D1){
> =09 print "$f -> $j factor of P($i)=3D$P\n";
> =09}
> }
> # last if $i=3D=3D1000;
> }
>=20
>=20
> __END__
>=20
>=20
> This code gives incorrects results as -1 as factor of a positive
> integer or 5 as factor of a number not terminated in 5 or 0.
>=20
> I do a sample calculation using 'echo "factor(number)" | gp' and this
> gives correct results. =20
>=20
> TIA, best regards=20
>=20

Well I have a workaround, but the speed is so slow that is impractical.

#!/usr/local/bin/perl -w

use Math::BigInt;

for $i (0..1_000_000_000) {
$P =3D 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;
@f =3D `echo "factor($P)" | gp`;
# print "@f\n";
# @f2 =3D grep (/\[\d+\s/, @f);
# print "@f2\n";
@fact=3D map (/\[(\d+)\s/, @f);
# print "@fact\n";
for $j (@fact){
if ($j%10!=3D1){
print "$j factor of P($i)=3D$P\n";
}
}
if ($i % 10_000_000=3D=3D0) { print "."; }
# last if $i=3D=3D1000;
}

__END__

PS: use bignum; don't works here.
Best regards,

--=20
http://www.telecable.es/personales/gamo/
S=F3lo hay 10 tipos de personas, las que saben binario y las que no
perl -e 'print 111_111_111**2,"\n";'
--8323328-1598280312-1142646809=:27081--

Posted by Sisyphus on March 18, 2006, 2:20 am
Please log in for more thread options



.
.
>
>
> This code gives incorrects results as -1 as factor of a positive
> integer or 5 as factor of a number not terminated in 5 or 0.
>
> I do a sample calculation using 'echo "factor(number)" | gp' and this
> gives correct results.
>
I think you could have provided a simpler test case:

use warnings;
use Math::Pari;

$f1 = Math::Pari::factor(72);
print $f1, "\n";

$z = 4225760561;
$f2 = Math::Pari::factor($z);
print $f2, "\n";

$p = PARI $z;
$f3 = Math::Pari::factor($p);
print $f3, "\n";

__END__

(You could, of course, further simplify the above code.) For me that
produces:

[2,3;3,2]
[-1,1;5,1;13,1;743,1;1433,1]
[-1,1;5,1;13,1;743,1;1433,1]

Quite clearly the first line is correct, since (2**3) * (3**2) == 72
Just as clearly, the second 2 lines are not what we expect.

There's obviously a problem when we get to 32-bit numbers. The unsigned long
4225760561, when treated as a signed int, becomes -69206735 - and it's not
hard to work out that the last 2 lines of output are the factorisation
of -69206735.

I'm not surprised that factor($z) in the above code returns what we see, but
I *did* think that factor($p) would have returned what you and I expected. I
think it's a bug - though I'm not an expert on Math::Pari and haven't read
the documentation very thoroughly.

As for what to do about this .... you could file a bug report at
http://rt.cpan.org/Public/Dist/Display.html?Name=Math-Pari , or you could
contact the author directly. I don't think he would mind that. (I think he
peruses this list, so he may yet respond to this thread.)

Cheers,
Rob



Posted by Ilya Zakharevich on March 18, 2006, 2:32 am
Please log in for more thread options


[A complimentary Cc of this posting was sent to
gamo

> This code gives incorrects results as -1 as factor of a positive
> integer or 5 as factor of a number not terminated in 5 or 0.

How do you suppose we are going to treat this? Send you our
condolences? What else do you think we can do?

What version of what? What Math::Pari test suite was saying? Can you
shorten your program to 1 line?

Puzzled,
Ilya

Posted by gamo on March 18, 2006, 5:30 am
Please log in for more thread options


This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1710087361-1142677830=:22760
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Sat, 18 Mar 2006, Ilya Zakharevich wrote:

> [A complimentary Cc of this posting was sent to
> gamo=20
3094@jvz.es>:
>=20

Thank you.

> > This code gives incorrects results as -1 as factor of a positive
> > integer or 5 as factor of a number not terminated in 5 or 0.
>=20
> How do you suppose we are going to treat this? Send you our
> condolences? What else do you think we can do?
>=20
> What version of what? What Math::Pari test suite was saying? Can you

Math-Pari-2.010703.tar.gz

> shorten your program to 1 line?
>=20

No, I can't.

> Puzzled,
> Ilya
>=20

--=20
http://www.telecable.es/personales/gamo/
S=F3lo hay 10 tipos de personas, las que saben binario y las que no
perl -e 'print 111_111_111**2,"\n";'
--8323328-1710087361-1142677830=:22760--

Similar ThreadsPosted
Math::Pari headaches August 15, 2006, 1:50 pm
Memory Leak in Math::Pari December 16, 2004, 6:21 am
Building Math::Pari using Sun Studio 11 October 16, 2006, 11:46 am
math::pari on solaris 9 make failure December 8, 2004, 6:36 am
Math::Pari doesn't compile with GCC 3.4.2 in Solaris 9 sparc January 17, 2005, 5:24 pm
Newbie Desperate for Help Installing Math::Pari August 9, 2006, 10:25 pm
Math-Pari-2.010501- make - cannot exec 'cc1' September 26, 2004, 11:09 am
Failure to build Math::Pari Solaris 10 Studio 11 November 7, 2006, 1:28 pm
Segmentation violation in Math:Pari when used in connection with Thread:Pool February 2, 2006, 4:18 am
what is wrong in this code November 13, 2005, 10:40 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap