Fractional Parts

The digits of the fractional part of a number x expressed in base r are obtained by repeatedly multiplying by r and taking the integer part. The following function gives a list of the first d digits in the expansion of a fractional part  x in base r. x is originally in base 10. This algorithm can also be expressed recursively in a very natural way.

Clear[RR]

RR[x_, r_, d_] := RR[x, r, {}, d]

RR[x_, r_, l_, d_] := RR[FractionalPart[r x], r, Append[l, IntegerPart[r x]], d - 1]

RR[x_, r_, l_, 0] := l

Here we try it with the fractional part of π.

RR[Pi - 3, 2, 20]

{0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0}


Created by Mathematica  (June 3, 2006) Valid XHTML 1.1!