Digit Representations

Helmut Knaust, Department of Mathematical Sciences, UTEP, El Paso TX 79968
hknaust@utep.edu

Last edits: 02/13/2025

This command generates the generalized “decimal” representation of a given rational number. 
DigitRepresentation[r,b] takes as input a rational number  r (as a fraction) and an optional base b (default is 10). The base b must be an integer between 2 and 36. Integers beyond 9 are denoted by letters a,...,z.

DigitRepresentation[fraction_Rational,base_Integer:10]:=Module[{nr=fraction,b,d1,d2,d3,fp,d,pos},b=base;d1=IntegerString[IntegerPart[nr],b];nr=nr-IntegerPart[nr];fp={};d={};
While[Not[MemberQ[fp,nr]],fp=Append[fp,nr];nr=b nr;d=Append[d,IntegerPart[nr]];nr=nr-IntegerPart[nr];];
pos=Position[fp,nr][[1,1]];d2=If[pos>1,d[[1;;pos-1]],{}];d3=d[[pos;;Length[d]]];
Print[If[b==10,"","("],d1,".",If[d2=={},"",IntegerString[FromDigits[d2,b],b,Length[d2]]],If[d3=={}||d3=={0},"",OverBar[IntegerString[FromDigits[d3,b],b,Length[d3]]]],If[b==10,"",")"],If[b==10,"",Subscript["",b]]];]

DigitRepresentation[1/7]

DigitRepresentation[1/7,11]

DigitRepresentation[1/77,11]

DigitRepresentation[11/7,11]

DigitRepresentation[1/9,3]

DigitRepresentation[38370/9,23]

Checking the last example:

N[38370/9,20]
N[8 (23^2)+1 (23^1) + 8 (23^0)+ 7 (23^(-1))+15 (23^(-2)),20]
N[8 (23^2)+1 (23^1) + 8 (23^0)+ 7 (23^(-1))+15 (23^(-2))+7 (23^(-3))+15 (23^(-4)),20]

The default base is 10:

DigitRepresentation[1/7]

The expansion of 1/7 in various bases:

Table[DigitRepresentation[1/7,k],{k,2,20}];