Charles Greathouse on Tue, 03 Dec 2013 17:26:04 +0100

 Re: reverse of digits()

I think there's a reason, but not a good reason: the current code uses convi to convert the number to base 10^9, so of course this is base-10 specific. (I guess this is memory-inefficient for 64-bit systems?)

Charles Greathouse
Analyst/Programmer
Case Western Reserve University

On Tue, Dec 3, 2013 at 10:48 AM, Max Alekseyev wrote:
Btw, is there a reason why sumdigits(), unlike digits(), does not have
the second optional argument of base?

Thanks,
Max

On Wed, Oct 17, 2012 at 9:01 PM, Mathieu Carbou
<mathieu.carbou@gmail.com> wrote:
> Hello,
>
> I've seen the new digits() and sumdigits() functions. I was wondering if
> there was also the reverse function of digits (i.e. vectonum([1,2,3,4,5],
> 10) == 12345) plus these functions which could be useful ;-)
>
> Thank you !
>
> /*
>     Digit decomposition of n in base b by ascending order of b exponents.
> I.e. digits(12345) == [1,2,3,4,5]
> */
> vectonum(v, b=10) =
> {
>     my(n=0);
>     for(i=1,#v, n+=v[i]*b^(#v-i));
>     return(n);
> }
>
> /*
>     Number of digits of n in base b
> */
> ndigits(n, b=10) =
> {
>     my(c=0);
>     if(n==0, return(1));
>     while(n, n=n\b; c++);
>     return(c);
> }
>
> /*
>     Representation of n in factoradic base b.
>     The number n is taken modulo b!
> */