Digit Representations

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

Last edits: 02/10/2026

This command generates the generalized “decimal” representation of a given rational number. 
DigitRepresentation[r,b] takes as input a rational number  r (in decimal representation) and an optional base b (default is 10). The base b must be an integer between 2 and 36. 
Digits exceeding 9 are written as letters: a is 10, b is 11, etc.

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,5]

DigitRepresentation[1/7,3]

DigitRepresentation[1/7,11]

DigitRepresentation[(1/7)/11,11]

DigitRepresentation[1/9,3]

DigitRepresentation[38370/9,23]

The default base is 10:

DigitRepresentation[1/7]

Etc.

Cyclic reordering of the digits in k/7:
In base 10:

n=7;Table[DigitRepresentation[k/n],{k,1,n-1}];

In base 3:

n=7;Table[DigitRepresentation[k/n,3],{k,1,n-1}];

(1/7) in different bases:

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

The list of the alphabetic digits:

Table[{n,BaseForm[n,36]},{n,2,35}]//TableForm