Tips on PARI/gp

/* 
   Sum of the digits of n in p-adic expansion.
   For example, if p=3, n=100, then 
     100 = 1 + 0*3 + 2*3^2 + 3^4, 
     Sp(100,3) = 1 + 0 + 2 + 3 = 6.
*/
Sp(n,p)={sp=0;r=n;while(n<>0,r=n%p;n=(n-r)/p;print(n,",",r);sp+=r;);return(sp);};