Bill Allombert on Fri, 09 Dec 2005 21:31:27 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Factoring and gcoeff()


On Fri, Dec 09, 2005 at 09:05:25PM +0100, Alessio Rocchi wrote:
> Hi everybody.
> Before beginning to explain my problem, i would really say thanks to Bill 
> Alombert for all the (wonderful) help he gave me until now.
> Ok, let's go on: i can succesfully factor numbers using factor() function 
> with libpari.
> I need now to insert all prime factors (and their exponents) into a long 
> int array (long int*. I know that it would be really simpler to use Pari 
> types, but i need to use just pure C long int pointers).
> Well, I use (pseudo-)code like this:
> 
> typedef struct{
> long int* factors;
> long int* exps;
> } Factors;
> 
> GEN F=factor(<number>);
> long length=lg(F[1]);
> //code for allocating Factors.factors[] and Factors.exps[]
> for(int i=0; i<length; i++){
>    Factors.factors[i]=itos(gcoeff(F, i, 1));
>    Factors.exps[i]=itos(gcoeff(F, i, 2));
> }

lg(F[1]) is the length of the _object_ F[1], which is one more than the
length of the _columns vector_ F[1]. Also indices start at 1 in PARI,
as in GP, not at 0, so gcoeff(F, 0, 1) is random garbage, hence
itos(gcoeff(F, 0, 1)) will segfault.

You probably want to do:

GEN F=factor(<number>);
long length=lg(F[1])-1;
//code for allocating Factors.factors[] and Factors.exps[]
for(int i=0; i<length; i++){
   Factors.factors[i]=itos(gcoeff(F, i+1, 1));
   Factors.exps[i]=itos(gcoeff(F, i+1, 2));
}

Please try to send examples that can be compiled, they are much easier to
debug.

Cheers,
Bill.