Max Alekseyev on Tue, 03 Dec 2013 16:48:44 +0100


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

Re: reverse of digits()


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!
> */
> factoradic(n, b=10) =
> {
>     my(v=vector(b), qr=[0,n%(b!)]);
>     for(i=1, b, qr=divrem(qr[2], (b-i)!); v[i]=qr[1]);
>     return(v);
> }
>
>
> --
>
> Mathieu Carbou
> Cell: 514-660-4287