Re: Code to translate to negative base...
- To: mathgroup at smc.vnet.net
- Subject: [mg64591] Re: Code to translate to negative base...
- From: Cca <cca at gregosetroianos.mat.br>
- Date: Thu, 23 Feb 2006 00:34:51 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>Does anyone have code to translate to a negative base?
Surprinsingly enough, this is very simple, although not much known among students. I find the approach below conceptually very intuitive and ellegant: everything depends on a REMAINDER function.
Suppose that you already have implemented the traditional algorithm that gives you the digits of a integer number N in base B, for B>0 in Integers. Then, just enter with a negative value for B and you will get what you want.
Let me ellaborate on this a little. We can see the traditional algorithm as a Mathematica command, say (although below I´ll not give any formatting instructions)
myBaseForm[A, B, r]
where r is a REMAINDER FUNCTION, that is, a rule
r[A, B]
such that A is congruent to r[A,B] mod B. The Euclidean remainder -- the "standard" one -- is given by
r[A_, B_] := A - Abs[B]*Floor[A/Abs[B]]
or, using Mathematica Mod,
r[A_, B_] := Mod[A, Abs[B]]
For each A in Integers, this gives the UNIQUE number r such that A is congruent to r mod B and 0<=r<Abs[B].
So, using the notations above, he following would always return True:
myBaseForm[A, B] == BaseForm[A, B, Mod[#1, Abs[#2]] & ]
This means that what you want is just
myBaseForm[A, -B]
Here is a direct implementation of the ideas above:
myBaseForm[a_, base_, r_:(Mod[#1, Abs[#2]] & )] :=
Reverse[Drop[(r[#1, base] & ) /@
FixedPointList[(#1 - r[#1, base])/base & ,
a], -2]]
Some examples for comparison:
IntegerDigits[200, 3]
{2,1,1,0,2}
myBaseForm[200, 3]
{2,1,1,0,2}
myBaseForm[21, -10]
{1,8,1}
Carlos César de Araújo
Gregos & Troianos Educacional
www.gregosetroianos.mat.br
Belo Horizonte, MG, Brasil
(31) 3283-1122