Bill Allombert on Fri, 06 Feb 2015 00:15:33 +0100


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

Re: fordiv question


On Thu, Feb 05, 2015 at 09:46:06PM +0100, Karim Belabas wrote:
> With the correct expression:
>   ? forvec(X=[[0,240],[0,50],[0,20],[0,15]], x=2^X[1]*3^X[2]*5^X[3]*7^X[4])
>   time = 2,653 ms.
> 
> Better (and faster):
>   ? P = [2,3,5,7];
>   ? forvec(X=[[0,240],[0,50],[0,20],[0,15]], x=factorback(P,X))
>   time = 1,821 ms.

Actually you can do better in theory by using a cache, but this
remove much of the simplicity of forvec and it is probably slower
in GP:
{
  P=[2,3,5,7];
  pr=[1,1,1,1];
  my(s);
  forvec(X=[[0,240],[0,50],[0,20],[0,15]],
    my(j=#X);
    while(j > 0 && X[j]==0,j--);
    if (j==0,x=1,
      pr[j]*=P[j];
      for(k=j+1,#X,pr[k]=pr[j]);
      x=pr[j]);
   s++);
   s
}
%1 = 4129776
? ##
  ***   last result computed in 4,212 ms.

Cheers,
Bill