Re: Constructing a number from digits in an arbitrary base

2020-11-02 Thread Brad Gilbert
I believe this is what you were looking for

sum 46, 20, 26, 87, 11 Z* (1, 101, 101² … *)

sum 1234567890.polymod(101 xx *) Z* (1, 101, 101² … *)

On Mon, Nov 2, 2020 at 2:12 PM Sean McAfee  wrote:

> On Fri, Oct 30, 2020 at 2:19 PM Elizabeth Mattijsen 
> wrote:
>
>> > On 30 Oct 2020, at 22:11, Sean McAfee  wrote:
>> > With polymod, I can get the values of digits even in large bases like
>> 101:
>> >
>> > > 1234567890.polymod(101 xx *)
>> > (46 20 26 87 11)
>> >
>> > Given a list of digit values like that, and the base, I want to
>> reconstruct the original number.
>
>
> So you'd basically need a sub that takes a List, and a base factor, and
>> does the necessary arithmetic for you.  I don't think that's in core.  I'd
>> be glad if someone proved me wrong  :-)
>>
>
> Found it!  The "trick" is to use square braces after the colon and base.
> For example, :101[11,87,26,20,46] evaluates to 1234567890.
>
> The digit values have to be supplied most-significant to
> least-significant, the opposite of the order returned by polymod, and the
> base must be known at compile time, but this is definitely the construction
> I was trying to recall originally.  Even nicer for my golfing purposes, the
> list elements are coerced to numbers automatically, so for example :2[True,
> False, True, False] evaluates to 10.
>
> I can't locate any documentation on this feature, but then I couldn't find
> documentation on the dynamic string-parsing version (eg. :2($str)) when I
> looked either, only the literal form, like :2<10101>.
>
>


Re: Constructing a number from digits in an arbitrary base

2020-11-02 Thread Sean McAfee
On Fri, Oct 30, 2020 at 2:19 PM Elizabeth Mattijsen  wrote:

> > On 30 Oct 2020, at 22:11, Sean McAfee  wrote:
> > With polymod, I can get the values of digits even in large bases like
> 101:
> >
> > > 1234567890.polymod(101 xx *)
> > (46 20 26 87 11)
> >
> > Given a list of digit values like that, and the base, I want to
> reconstruct the original number.


So you'd basically need a sub that takes a List, and a base factor, and
> does the necessary arithmetic for you.  I don't think that's in core.  I'd
> be glad if someone proved me wrong  :-)
>

Found it!  The "trick" is to use square braces after the colon and base.
For example, :101[11,87,26,20,46] evaluates to 1234567890.

The digit values have to be supplied most-significant to least-significant,
the opposite of the order returned by polymod, and the base must be known
at compile time, but this is definitely the construction I was trying to
recall originally.  Even nicer for my golfing purposes, the list elements
are coerced to numbers automatically, so for example :2[True, False, True,
False] evaluates to 10.

I can't locate any documentation on this feature, but then I couldn't find
documentation on the dynamic string-parsing version (eg. :2($str)) when I
looked either, only the literal form, like :2<10101>.