Дмитрий Рыбас on Wed, 17 Jul 2019 11:58:01 +0200


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

Re: Using local variable for memoizing in recursion


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]);
>}

Regards,
Dmitry.