Ruud H.G. van Tol on Sun, 15 Oct 2023 12:46:58 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: digits(n,-4) |
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 ) % 4Note 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. Intermediate result: ? digits2( x, b=10 ) = { if( b >= 0, return(digits(x,b)) ); if( -2 == b, return( digits( bitxor( x + 0xAAAAAAAA, 0xAAAAAAAA ), 2 ) ) , -4 == b, return( digits( bitxor( x + 0xCCCCCCCC, 0xCCCCCCCC ), 4 ) ) ); my( v = [] ); while( x = divrem( x, -4 ) , v = concat( x[2], v ); x = x[1] ); v; }
Should digits() be changed to support a negative base?There are too many variants...
ACK. -- Ruud https://en.wikipedia.org/wiki/Negative_base (refers to Schroeppel)