# Math::Pari 'factor' is wrong

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

#!/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

## Re: Math::Pari 'factor' is wrong

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:

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 = 5*\$i**4-10*\$i**3+20*\$i*\$i-15*\$i+11;
@f = `echo "factor(\$P)" | gp`;
#    print "@f\n";
#    @f2 = 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--

## Re: Math::Pari 'factor' is wrong

.
.

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.

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

## Re: Math::Pari 'factor' is wrong

[A complimentary Cc of this posting was sent to
gamo

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

## Re: Math::Pari 'factor' is wrong

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:

Thank you.

Math-Pari-2.010703.tar.gz

No, I can't.

--=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--

## Re: Math::Pari 'factor' is wrong

[A complimentary Cc of this posting was sent to
gamo

This does not make sense.  Why do approximate calculations, if you
want integer arithmetic?

The correct way is (if \$i fits exact Perl types)

for \$j (0..1_000_000_000) {
my \$i = PARI \$j;        # Or PARI "\$j" to be absolutely sure
\$P = ...

Or make a while() loop operating on a Math::Pari integer, not Perl integer.

But anyway, I discovered a bug in unsigned --> PARI conversion.
Sigh...

perl -MMath::Pari -wle "print PARI 3936116531"
-358850765

This is clearly a bug, Perl data contains enough bits of info to
deduce the correct result...  >>TODO

Hope this helps,
Ilya

## Re: Math::Pari 'factor' is wrong

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Ilya Zakharevich

[Note that this bug is not related to the problems the poster had
due to misunderstanding of Perl arithmetic; but it may move the
threshold where problems appear.]

I released 2.010704 which has this bug fixed.  (The test suite is
sensitive to details of propagation from INT to FLOAT in Perl, so may
be wrong on 64-bit platforms.  Also, if somebody runs a platform where
\$Config < \$Config, I would like to hear from you
even if test suite succeeds.  ;-)

Thanks,
Ilya

## Re: Math::Pari 'factor' is wrong

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-1718442122-1142679533=:22760
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Sat, 18 Mar 2006, Ilya Zakharevich wrote:

That's not an approximate calculation. \$P It's the value of the polynomial=
=20
in the point \$i.=20

Ok, I'll revise that.

Thanks, all the best

--=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-1718442122-1142679533=:22760--

## Re: Math::Pari 'factor' is wrong

[A complimentary Cc of this posting was sent to
gamo

Says who?

It IS an approximate calculation.  Any calculation done with Perl
numeric values is approximate.  The loss of precision may happen to be
0, but this is a coincidence only.

Hope this helps,
Ilya