Karim Belabas on Wed, 17 Jul 2019 12:03:09 +0200


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

Re: Using local variable for memoizing in recursion


* Дмитрий Рыбас [2019-07-17 11:58]:
> Karim,
> 
> >Here's my take:
> 
> >
> >\\ vecsum(pnkv_4(n,k)), assuming n > 1
> >pnk_4(n,k) =
> >{ my(k1 = k+1, kx = k1, a = 1, t = vector(k1, i, vector(k)));
> >  t[k1][1] = 1;
> >  for (i = 1, n-1,
> >    my(ky = a);
> >    t[a] = vector(k, j, if (!(ky--), ky = k1);
> >                        if (j == 1, t[ky][1]
> >                                  , t[kx][j-1] + t[ky][j]));
> >    kx = a; if (a++ > k1, a = 1));
> 
> -------------------------------^
> 
> if a==1, then vecsum(t[a-1]) below won't work :-) That's a case for
> example for pnk_4(4,2)
> 
> >  vecsum(t[a-1]);
> >}

Indeed, Mike Day pointed this out¹. The vecsum should be replaced by

   vecsum(t[kx]);

Cheers,

    K.B.

¹: http://pari.math.u-bordeaux.fr/archives/pari-users-1907/msg00017.html
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 21 23
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`