Bill Allombert on Sun, 08 Jan 2023 23:26:48 +0100


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

Re: lookup-optimization


On Sun, Jan 08, 2023 at 09:48:29PM +0100, Ruud H.G. van Tol wrote:
> A243115( N=575, V=0 )= {
>   my
>   ( r= []  /* result */
>   , s= []  /* lookup-set */
>   , t= [ [1,2,1,1], [2,1,1,0] ]  /* detailed table */
>   );
> 
>   forstep
>   ( v2= 3, N, 4
>   , my( found= 0 );
>     for /* Look up v2-class in s */
>     ( i=1, #s
>     , if
>       ( s[i][2] == v2 % 2^s[i][1]
>       , found= 1;
>         break;
>       );
>     );
>     if( found, next );

You could replace break by next(2) and get rid of found.

You could replace s by a Map: v2 -> p2:

s = Map()
...
 for(i=1,logint(v2,2)+1,
   my(nv2=v2%2^i,p2);
   if (mapisdefined(s,nv2,&p2) && nv2==v2%2^p2, next(2)));
...
mapput(~s,v2,p2); \\instead of s=Set(...)

You should get below 1s for A243115(10^5);

Cheers,
Bill