Books Number Theory

Representations of Integers

Our ancestors learned to count early on. Fingers run out at 1010, and that is why they invented positional notation.

Theorem 2.1

Let ๐‘โˆˆZ+๐‘\in {\mathbb{Z}}^{+} with ๐‘โ‰ฅ2๐‘\geq 2. Every positive integer ๐‘›๐‘› can be written uniquely in the form

๐‘›=๐‘Ž๐‘˜๐‘๐‘˜+๐‘Ž๐‘˜โˆ’1๐‘๐‘˜โˆ’1+โ‹ฏ+๐‘Ž1๐‘+๐‘Ž0,๐‘›={๐‘Ž}_{๐‘˜}{๐‘}^{๐‘˜}+{๐‘Ž}_{๐‘˜-1}{๐‘}^{๐‘˜-1}+\cdots +{๐‘Ž}_{1}๐‘+{๐‘Ž}_{0},

where ๐‘˜โˆˆN๐‘˜\in \mathbb{N}, ๐‘Ž๐‘–โˆˆZ{๐‘Ž}_{๐‘–}\in \mathbb{Z} and 0โ‰ค๐‘Ž๐‘–<๐‘0\leq {๐‘Ž}_{๐‘–}<๐‘ for ๐‘–=0,1,โ€ฆ,๐‘˜๐‘–=0,1,\ldots ,๐‘˜, and ๐‘Ž๐‘˜โ‰ 0{๐‘Ž}_{๐‘˜}\neq 0.

The digits are the remainders from Algorithm 2.1.

Algorithm 2.1

Base-๐‘๐‘ expansion

Input
๐‘›โˆˆZ+๐‘›\in {\mathbb{Z}}^{+} and ๐‘โˆˆZ+๐‘\in {\mathbb{Z}}^{+} with ๐‘โ‰ฅ2๐‘\geq 2
Output
๐‘Ž0,โ€ฆ,๐‘Ž๐‘˜{๐‘Ž}_{0},\ldots ,{๐‘Ž}_{๐‘˜} with ๐‘Ž๐‘˜โ‰ 0{๐‘Ž}_{๐‘˜}\neq 0
1 ๐‘ž0โ†๐‘›{๐‘ž}_{0}โ†๐‘›
2 ๐‘กโ†0๐‘กโ†0
3 while ๐‘ž๐‘ก>0{๐‘ž}_{๐‘ก}>0 do
4 (๐‘ž๐‘ก+1,๐‘Ž๐‘ก)โ†(โŒŠ๐‘ž๐‘ก๐‘โŒ‹,๐‘ž๐‘กmodโก๐‘)({๐‘ž}_{๐‘ก+1},{๐‘Ž}_{๐‘ก})โ†\left(\lfloor \frac{{๐‘ž}_{๐‘ก}}{๐‘}\rfloor ,{๐‘ž}_{๐‘ก}\operatorname{mod}๐‘\right) // 0โ‰ค๐‘Ž๐‘ก<๐‘0\leq {๐‘Ž}_{๐‘ก}<๐‘
5 ๐‘กโ†๐‘ก+1๐‘กโ†๐‘ก+1
6 ๐‘˜โ†๐‘กโˆ’1๐‘˜โ†๐‘ก-1
Proof

Existence. Just before each test of the guard on line 3, the invariant ๐‘ƒ(๐‘ก)๐‘ƒ(๐‘ก) is the conjunction of three clauses.

  1. Expansion. ๐‘›=โˆ‘๐‘–=0๐‘กโˆ’1๐‘Ž๐‘–๐‘๐‘–+๐‘ž๐‘ก๐‘๐‘ก๐‘›={\sum }_{๐‘–=0}^{๐‘ก-1}{๐‘Ž}_{๐‘–}{๐‘}^{๐‘–}+{๐‘ž}_{๐‘ก}{๐‘}^{๐‘ก}. The sum is empty, hence 00, when ๐‘ก=0๐‘ก=0.
  2. Digits. 0โ‰ค๐‘Ž๐‘–<๐‘0\leq {๐‘Ž}_{๐‘–}<๐‘ for every ๐‘–<๐‘ก๐‘–<๐‘ก.
  3. Bound. ๐‘ž๐‘กโ‰ฅ0{๐‘ž}_{๐‘ก}\geq 0.

The variant is ๐‘ž๐‘ก{๐‘ž}_{๐‘ก}. Initialization and maintenance keep ๐‘ƒ(๐‘ก)๐‘ƒ(๐‘ก) true at every test of the guard. Termination shows that the guard fails. Exit reads the expansion off ๐‘ƒ(๐‘ก)๐‘ƒ(๐‘ก) at that test.

Initialization. After lines 1 and 2, and before the guard is tested, the expansion says ๐‘›=๐‘ž0๐‘›={๐‘ž}_{0}. The digit clause is vacuous. The bound holds because ๐‘ž0=๐‘›>0{๐‘ž}_{0}=๐‘›>0.

Maintenance. Assume ๐‘ƒ(๐‘ก)๐‘ƒ(๐‘ก) and the guard on line 3, and execute lines 4 and 5 once. The new digit satisfies 0โ‰ค๐‘Ž๐‘ก<๐‘0\leq {๐‘Ž}_{๐‘ก}<๐‘, and the earlier digits are unchanged, so the digit clause holds at ๐‘ก+1๐‘ก+1. The new quotient meets the bound. If ๐‘ž๐‘ก+1โ‰คโˆ’1{๐‘ž}_{๐‘ก+1}\leq -1, then

๐‘ž๐‘ก=๐‘๐‘ž๐‘ก+1+๐‘Ž๐‘กโ‰คโˆ’๐‘+(๐‘โˆ’1)=โˆ’1<0,{๐‘ž}_{๐‘ก}=๐‘{๐‘ž}_{๐‘ก+1}+{๐‘Ž}_{๐‘ก}\leq -๐‘+(๐‘-1)=-1<0,

contradicting ๐‘ž๐‘ก>0{๐‘ž}_{๐‘ก}>0. The expansion carries to the next test of the guard:

๐‘›=โˆ‘๐‘–=0๐‘กโˆ’1๐‘Ž๐‘–๐‘๐‘–+๐‘ž๐‘ก๐‘๐‘ก=โˆ‘๐‘–=0๐‘กโˆ’1๐‘Ž๐‘–๐‘๐‘–+(๐‘๐‘ž๐‘ก+1+๐‘Ž๐‘ก)๐‘๐‘ก=โˆ‘๐‘–=0๐‘กโˆ’1๐‘Ž๐‘–๐‘๐‘–+๐‘Ž๐‘ก๐‘๐‘ก+๐‘ž๐‘ก+1๐‘๐‘ก+1=โˆ‘๐‘–=0๐‘ก๐‘Ž๐‘–๐‘๐‘–+๐‘ž๐‘ก+1๐‘๐‘ก+1.\begin{aligned}๐‘› & =\sum _{๐‘–=0}^{๐‘ก-1}{๐‘Ž}_{๐‘–}{๐‘}^{๐‘–}+{๐‘ž}_{๐‘ก}{๐‘}^{๐‘ก} \\ & =\sum _{๐‘–=0}^{๐‘ก-1}{๐‘Ž}_{๐‘–}{๐‘}^{๐‘–}+(๐‘{๐‘ž}_{๐‘ก+1}+{๐‘Ž}_{๐‘ก}){๐‘}^{๐‘ก} \\ & =\sum _{๐‘–=0}^{๐‘ก-1}{๐‘Ž}_{๐‘–}{๐‘}^{๐‘–}+{๐‘Ž}_{๐‘ก}{๐‘}^{๐‘ก}+{๐‘ž}_{๐‘ก+1}{๐‘}^{๐‘ก+1} \\ & =\sum _{๐‘–=0}^{๐‘ก}{๐‘Ž}_{๐‘–}{๐‘}^{๐‘–}+{๐‘ž}_{๐‘ก+1}{๐‘}^{๐‘ก+1}.\end{aligned}

Thus ๐‘ƒ(๐‘ก+1)๐‘ƒ(๐‘ก+1) holds.

Termination. The bound clause says the variant ๐‘ž๐‘ก{๐‘ž}_{๐‘ก} is a nonnegative integer. Whenever the guard holds, the body replaces it by a strictly smaller one. Maintenance has already given ๐‘ž๐‘ก+1โ‰ฅ0{๐‘ž}_{๐‘ก+1}\geq 0, and

๐‘ž๐‘กโˆ’๐‘ž๐‘ก+1=(๐‘โˆ’1)๐‘ž๐‘ก+1+๐‘Ž๐‘ก.{๐‘ž}_{๐‘ก}-{๐‘ž}_{๐‘ก+1}=(๐‘-1){๐‘ž}_{๐‘ก+1}+{๐‘Ž}_{๐‘ก}.

If ๐‘ž๐‘ก+1โ‰ฅ1{๐‘ž}_{๐‘ก+1}\geq 1, the right-hand side is at least ๐‘โˆ’1โ‰ฅ1๐‘-1\geq 1. If ๐‘ž๐‘ก+1=0{๐‘ž}_{๐‘ก+1}=0, it equals ๐‘Ž๐‘ก=๐‘ž๐‘กโ‰ฅ1{๐‘Ž}_{๐‘ก}={๐‘ž}_{๐‘ก}\geq 1. Thus ๐‘ž๐‘ก+1<๐‘ž๐‘ก{๐‘ž}_{๐‘ก+1}<{๐‘ž}_{๐‘ก}. If the guard never failed, the successive values of the variant would be a strictly decreasing sequence of nonnegative integers, whose set of values would have no least element, contradicting Proposition 1.1. So the guard fails after finitely many iterations.

Exit. The guard on line 3 has failed, so ๐‘ž๐‘ก=0{๐‘ž}_{๐‘ก}=0, and ๐‘ƒ(๐‘ก)๐‘ƒ(๐‘ก) still holds. The expansion collapses to

๐‘›=โˆ‘๐‘–=0๐‘กโˆ’1๐‘Ž๐‘–๐‘๐‘–,๐‘›=\sum _{๐‘–=0}^{๐‘ก-1}{๐‘Ž}_{๐‘–}{๐‘}^{๐‘–},

and the digit clause gives 0โ‰ค๐‘Ž๐‘–<๐‘0\leq {๐‘Ž}_{๐‘–}<๐‘. Here ๐‘กโ‰ฅ1๐‘ก\geq 1: the case ๐‘ก=0๐‘ก=0 would mean ๐‘ž0=๐‘›=0{๐‘ž}_{0}=๐‘›=0. Line 6 sets ๐‘˜=๐‘กโˆ’1๐‘˜=๐‘ก-1. The last pass through line 4 began under the guard ๐‘ž๐‘˜>0{๐‘ž}_{๐‘˜}>0 and produced ๐‘ž๐‘˜+1=0{๐‘ž}_{๐‘˜+1}=0, so ๐‘ž๐‘˜=๐‘Ž๐‘˜{๐‘ž}_{๐‘˜}={๐‘Ž}_{๐‘˜} and ๐‘Ž๐‘˜โ‰ 0{๐‘Ž}_{๐‘˜}\neq 0.

Uniqueness. Suppose โˆ‘๐‘–=0๐‘˜๐‘Ž๐‘–๐‘๐‘–{\sum }_{๐‘–=0}^{๐‘˜}{๐‘Ž}_{๐‘–}{๐‘}^{๐‘–} and โˆ‘๐‘–=0๐‘š๐‘๐‘–๐‘๐‘–{\sum }_{๐‘–=0}^{๐‘š}{๐‘}_{๐‘–}{๐‘}^{๐‘–} are two such expansions. Set ๐‘Ž๐‘–=0{๐‘Ž}_{๐‘–}=0 for ๐‘–>๐‘˜๐‘–>๐‘˜ and ๐‘๐‘–=0{๐‘}_{๐‘–}=0 for ๐‘–>๐‘š๐‘–>๐‘š, and let ๐‘=maxโก(๐‘˜,๐‘š)๐‘=\max (๐‘˜,๐‘š). Then

โˆ‘๐‘–=0๐‘(๐‘Ž๐‘–โˆ’๐‘๐‘–)๐‘๐‘–=0.\sum _{๐‘–=0}^{๐‘}({๐‘Ž}_{๐‘–}-{๐‘}_{๐‘–}){๐‘}^{๐‘–}=0.

Assume, on the contrary, that ๐‘Ž๐‘–โ‰ ๐‘๐‘–{๐‘Ž}_{๐‘–}\neq {๐‘}_{๐‘–} for some ๐‘–๐‘–, and let ๐‘Ÿ๐‘Ÿ be the least such index. The powers below ๐‘๐‘Ÿ{๐‘}^{๐‘Ÿ} cancel, so

๐‘Ž๐‘Ÿโˆ’๐‘๐‘Ÿ=โˆ’โˆ‘๐‘—=1๐‘โˆ’๐‘Ÿ(๐‘Ž๐‘Ÿ+๐‘—โˆ’๐‘๐‘Ÿ+๐‘—)๐‘๐‘—.{๐‘Ž}_{๐‘Ÿ}-{๐‘}_{๐‘Ÿ}=-\sum _{๐‘—=1}^{๐‘-๐‘Ÿ}({๐‘Ž}_{๐‘Ÿ+๐‘—}-{๐‘}_{๐‘Ÿ+๐‘—}){๐‘}^{๐‘—}.

The sum is empty when ๐‘Ÿ=๐‘๐‘Ÿ=๐‘, and in every case the right-hand side is a multiple of ๐‘๐‘. Thus ๐‘โˆฃ(๐‘Ž๐‘Ÿโˆ’๐‘๐‘Ÿ)๐‘โˆฃ({๐‘Ž}_{๐‘Ÿ}-{๐‘}_{๐‘Ÿ}). But 1โ‰คโˆฃ๐‘Ž๐‘Ÿโˆ’๐‘๐‘Ÿโˆฃโ‰ค๐‘โˆ’11\leq |{๐‘Ž}_{๐‘Ÿ}-{๐‘}_{๐‘Ÿ}|\leq ๐‘-1, so ๐‘โˆค(๐‘Ž๐‘Ÿโˆ’๐‘๐‘Ÿ)๐‘โˆค({๐‘Ž}_{๐‘Ÿ}-{๐‘}_{๐‘Ÿ}). This is a contradiction. Therefore ๐‘Ž๐‘–=๐‘๐‘–{๐‘Ž}_{๐‘–}={๐‘}_{๐‘–} for every ๐‘–๐‘–. In particular ๐‘๐‘˜=๐‘Ž๐‘˜โ‰ 0{๐‘}_{๐‘˜}={๐‘Ž}_{๐‘˜}\neq 0 and ๐‘Ž๐‘š=๐‘๐‘šโ‰ 0{๐‘Ž}_{๐‘š}={๐‘}_{๐‘š}\neq 0, so ๐‘˜=๐‘š๐‘˜=๐‘š.

In particular, setting ๐‘=2๐‘=2, we get the following corollary, the binary expression of a positive integer. It is very common in computer science.

Corollary 2.1
Every positive integer can be written uniquely as a sum of distinct powers of 22.

๐‘๐‘ is called the base, or radix, of the expansion. We use the base-1010 expansion as our canonical way of representing a positive integer, and we call it decimal notation. Base-22 expansions are called binary expansions, base-88 expansions are called octal expansions, and base-1616 expansions are called hexadecimal expansions, also very common in computer science.

Example 2.1

Binary digits are called bits in computer terminology. A byte is 88 bits. An ASCII letter is one byte. The word code consists of 44 letters.

ASCII decimal binary hex
c 9999 0110001101100011 6363
o 111111 0110111101101111 6f\text{6f}
d 100100 0110010001100100 6464
e 101101 0110010101100101 6565

If you write exactly the content code into a file sample.txt via

Terminal window
printf 'code' > sample.txt

then sample.txt is exactly 44 bytes, and you can view its hexadecimal representation with

Terminal window
xxd sample.txt
# 00000000: 636f 6465 code