Re: ReadDigits
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1760] Re: ReadDigits
- From: m.cafaro at agora.stm.it (Massimo)
- Date: Wed, 26 Jul 1995 00:50:12 -0400
- Organization: I.Net S.p.A.
In article <3ui5a6$231 at news0.cybernetics.net>, insshc at gsusgi2.gsu.edu
(Samuel H. Cox) wrote:
> The function RealDigits[x, b] returns a list of two items. For example,
>
> RealDigits[Pi //N]
> {{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}, 1}
>
> What is the inverse of this function? That is, given the base b, a list
> {...} of base b digits, and an interger n, how do we elegantly obtain x for
> which RealDigits[x, b] = {{...},n}?
>
> ==================================
> Samuel H. Cox
> insshc at gsusgi2.gsu.edu
> Department of Risk Management and Insurance
> Georgia State University
> PO Box 4036
> Atlanta, GA 30302-4036
> 404-651-4854 (o)
> 404-651-4219 (f)
The code below solve the problem. It can be improved to gain elegance.
Inv[b_,list_,n_]:=Module[{d,nber,x},
d=Dimensions[list][[1]];
nber=0;
Do[x=list[[i]];
nber=N[nber+x*10^(n-i),d],{i,d}];Print[nber]];
Here is the Mathematica session:
RealDigits[Pi//N]
{{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 9}, 1}
l=%[[1]]
{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 9}
Inv[b_,list_,n_]:=Module[{d,nber,x},
d=Dimensions[list][[1]];
nber=0;
Do[x=list[[i]];
nber=N[nber+x*10^(n-i),d],{i,d}];Print[nber]];
Inv[10,l,1]
3.141592653589793238
I hope this is ok. Bye.
Massimo Cafaro
m.cafaro at agora.stm.it