Integers

The quotients of consecutive powers of the radix r are given by the remainders of successive divisions of the given number n by r. The function R gives a list with the digits of the number n in base (radix) r. n is given in base 10.

Clear[R]

R[n_, r_] := R[n, r, {}]

R[n_, r_, l_] := R[⌊n/r⌋, r, Prepend[l, Mod[n, r]]]

R[0, r_, l_] := l ;

A few examples expressing the number 239 in different bases:

R[239, 10]

{2, 3, 9}

R[239, 5]

{1, 4, 2, 4}

R[239, 20]

{11, 19}

R[239, 2]

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

The following example uses the letters of the alphabet to encode the digits (ref. "A course in number theory and cryptography", by Neal Koblitz).

R[10^6, 26]

{2, 4, 23, 7, 14}

FromCharacterCode[% + 65]

CEXHO

One million in binary.

R[10^6, 2]

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

One million in base 7.

R[10^6, 7]

{1, 1, 3, 3, 3, 3, 1, 1}

The number of digits a number's expression in base r is given by (log )/(log )⌋+1; for example, the number of digits of one million in base 7 is:

Ndigits[n_, r_] := ⌊Log[n]/Log[r] ⌋ + 1

Ndigits[10^6, 7]

8


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