Bill Allombert on Sun, 15 Oct 2023 13:36:09 +0200
|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
- To: pari-users@pari.math.u-bordeaux.fr
- Subject: Re: digits(n,-4)
- From: Bill Allombert <Bill.Allombert@math.u-bordeaux.fr>
- Date: Sun, 15 Oct 2023 13:36:01 +0200
- Arc-authentication-results: i=1; smail; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1697369753; c=relaxed/relaxed; bh=yQ/+BBc3wVFShwHOnIcawpkIzABHyT4Ol5LFa6fP4y4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:Mail-Followup-To: References:MIME-Version:Content-Type:Content-Disposition: In-Reply-To; b=glJ+xwjoFUVvuBGjgMwdHBVQG2ZqaOIwXF9GzfOjxQAk4hQx6qxoaAC8JmOTHUZTsXPFnuQyJx4cybt75KqLFKuezIPDk2G7pZ/8Fm9HhcgSdPd/p611ax3U/IfGZLuhEikcboV6dhxGqYfQpUhiqcTH0zSHVE+pO5aIugJcbh48lo/05DuAsuzmUBfLeJ4QcyXd3j9pdAf70iwXcM2YErKyyvBt9Jw5IBZfg9kTtjdCKq7wPS4YzBF/ApedNFWX2RRCcbt1LtwodByA+jJ/EsKswTwSSv0QX17H18tVnMvim+jiAK0WliTfj0Vx5MonV791yq55X4R59dkOs/UjSwp76JpQzvC9kGlJQ7eFwBwe5IjFUt07Ee3njfNpYa9efJPm4KyqtSZvWlIYacIk+nJNrWfOfk7PellUkW//RPw4s7LqsKcPduE4JnjGMuv0cmKtmdEgO3taqTEJEIzosGE9pnEpWA5OsoPa3wlsnoA+7F/X85tn+KrIrV+UxG9sZ7YM+5mJPtnckhJ/73gYrAFfHvYzLlEOHe7f/2AR5+DJ9LipIa2ZmN42KmZaDyBIgd4RsKjoS+2l0lGZJrzaveKugPwYuHnDSs0klQ68ROYRvPSmg007ddkXfKI9qDWZPCwCd0Z4wc0jNzU1mVdrWVwer5Ld1wQl3F5YwFhpgkI=
- Arc-seal: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1697369753; cv=none; b=tf6WzDaY5nLzUcQmRrXUO+7rP3gQHVlr1vjQ/wl0xJ/OI/sXd+HbBWcf67ySSy7Vf00FnM27oek13kTteyIFrDg6nU6Vv+E8UKycTNw5FRiMBEofPKKITIeUah+LS7P6YwRxw7yBPXbgAMBdeQ1pCLaM6c6C1XP/WWAS3Sspl53T0kAdZJIOmrXH8k/UyXRqFm9t1hDr4eQvO2rvcVlXYPGwxm+VSD7aLoEPTb3pz/E24xTYB4r/1UBgW6ZJTuKdCV1UMf2KaND7jTMaLnVxlbAn/pbjtpLcTVzpBENCq15ZMWBPftn/XFe8S4SdQICLLbPEJgn24wq4JH+ugOKpIWJ3CSb+knscrFR7lPnTqIvMtKFojmPhW5J2kCr/NelsJJIpp97nO15S8gRqxAuViTGXm9RBl6AoIM8FDCfrYHZmIrf0Rzg78g12mS52wKXMeG638Po6KdAo0lzO7BhUYVpfKDbghOINzNcxynHwzTTaqbuBi8kyjVWtd2RLTIUNQBIq8hSd5Royn3n3P0o8LgMXG4DlGFvYqJuVMJbuo5AKA/9exLbhq+4jM2mQmdj4tTfM6I9UegmTtL60X2RQ1//8ggqo8IO1mE1DTCGC5camS57oEYmhmwtDZ8Qge7oJzk5cWUVLfNCzDegnHZkNs0LrPvOuNejM9IisxWd4ODs=
- Authentication-results: smail; dmarc=none header.from=math.u-bordeaux.fr
- Authentication-results: smail; arc=none
- Delivery-date: Sun, 15 Oct 2023 13:36:09 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=math.u-bordeaux.fr; s=2022; t=1697369753; bh=yQ/+BBc3wVFShwHOnIcawpkIzABHyT4Ol5LFa6fP4y4=; h=Date:From:To:Subject:References:In-Reply-To:From; b=c7aHeMf5J2njxV85q1a52vNvxFHLgMnrRett4VwxbpnQVpJujJiGXaNbBrmUyzxbn OK8LDy+rmxFQlIto4bfLi/VXcwYAaHejeBxXEKlyptCoEZRAdQMqyZD+TUXiXvAINp 8+G/bDw8koPlNaFdl8nubO1kew7+Dr920wwKEMg5Sexjp4qUD7E9slpiQ2C6mr2Wei JNAh1VTX9fqdxQ5icxGr25iT6SmeCHD25B7vQ0DccbJusK7nM9mj4Ygd3u4Q1G8PwH nNJWvjcU0vFj88YhAkc0kJ8DU8ZHUMVQqA5Thmfqq+VyAbRUxsVtBrZpt+c0pExDkq in7Z4PdTLuR/K94PnLgEq3doQx8JLeXYyxz42fvxPftLY1BwGW1AfkJ4uQwiqGFi1q oa9GkB26TIkU5O0sOq2ymsB7m5u3TNrx3j7pG0OZ+VqGa5SnmAEdDZ5fAQkCRIa7Ps mzHqgAO8yyqhwxIAc1HNh0OokMOWsLY9yIHHcZvI2U5FqRGK5ijxYQ/ztUG9SnAoQw NYlIq63Kaib+5y8J8g2t0Ii9IAWKaXH8HOYj84pgnVVZEEyFYCjA9KsYEtvOUsJVt4 HN14nUioeYin+wqObx41hIXoV7ZrsYbM9tFqYvfDtbtMvR+Kqhpv8MZ0zJ6+ftdInR 6rDWn4NOSUkw2H7CjQaKOJgE=
- In-reply-to: <bb8a4249-733b-4c87-bb53-e26a5d24868e@isolution.nl>
- Mail-followup-to: pari-users@pari.math.u-bordeaux.fr
- References: <e26cea43-8ead-4ad4-ad99-87912f425538@freedom.nl> <ZSrfIN8IT4DkgiFx@seventeen> <bb8a4249-733b-4c87-bb53-e26a5d24868e@isolution.nl>
On Sun, Oct 15, 2023 at 12:46:48PM +0200, Ruud H.G. van Tol wrote:
>
> On 2023-10-14 20:34, Bill Allombert wrote:
> > On Sat, Oct 14, 2023 at 07:24:35PM +0200, Ruud H.G. van Tol wrote:
> > > ? fromdigits( [1,3,0], -4 )
> > > % 4
> > Note that we do need to know that the set of digits is here.
> >
> > > ? my(n=4, v=[]); while( n=divrem(n,-4), v=concat(n[2],v); n=n[1] ); v
> > > % [1, 3, 0]
> > >
> > > Is there a better way to do this?
> > I would start with use digits(n,16) and split the digits.
>
> Thanks, am still chewing on that.
I offer the following which should handle all cases and has the benefit of being
asymptotically fast.
? negdigits(-41,-4)
%1 = [1,2,3,3]
? negdigits(41,-4)
%2 = [3,2,1]
Cheers,
Bill.
negdigits(n,b)=
{
my(B=abs(b));
my(V=digits(abs(n),B));
if (b > 0, return(V));
my(carry=0);
if(n<0,V=-V);
for(i=0,#V-1,
my(u=V[#V-i]*(-1)^i+carry);
if (u<0,u+=B;carry=1,
u>=B,u-=B;carry=-1,
carry=0);
V[#V-i]=u);
if (carry>0, concat(carry,V),carry<0,concat([1,carry+B],V),V);
}