The earlier chapters separate three questions: what belongs to a set, how functions pair elements, and how a sequence assigns values to indices. Here those ideas meet arithmetic and computation. We first compare the standard number systems, then study the integer operations used by algorithms, and finally prove that iterative and recursive procedures do what they claim.

The prerequisites are Naive Set Theory, Functions and Mappings, and Sequences and Series. Throughout, N={0,1,2,…}\mathbb N=\{0,1,2,\ldots\}; positive integers are written Z>0\mathbb Z_{>0}. The treatments of infinite sets, recursive data, and invariants in Mathematics for Computer Science provide further practice [1][1] E. Lehman, F. T. Leighton, and A. R. Meyer, Mathematics for Computer Science. MIT OpenCourseWare, 2015. Revised May 18, 2015; infinite sets, recursive data, and invariants. https://ocw.mit.edu/courses/6-042j-mathematics-for-computer-science-spring-2015/mit6_042js15_textbook.pdf.

Number systems extend the available operations

The familiar inclusions are

N⊊Z⊊Q⊊R⊊C.\mathbb N\subsetneq\mathbb Z\subsetneq\mathbb Q \subsetneq\mathbb R\subsetneq\mathbb C.

Under these standard identifications, moving right retains the earlier numbers while permitting new solutions or limits.

SystemWhat it containsWhat the extension provides
N\mathbb N0,1,2,…0,1,2,\ldotsCounting and induction
Z\mathbb ZIntegers, positive, negative, and zeroSubtraction always stays in the system
Q\mathbb QRatios p/qp/q, with integers p,qp,q and q≠0q\ne0Division by any nonzero element
R\mathbb RRational and irrational real numbersA complete ordered field
C\mathbb CNumbers a+bia+bi, with a,b∈Ra,b\in\mathbb R and i2=−1i^2=-1A solution of x2=−1x^2=-1 and a broader algebraic setting

The first steps can be motivated by equations: x+3=1x+3=1 has no solution in N\mathbb N, and 2x=12x=1 has no solution in Z\mathbb Z. Passing from Q\mathbb Q to R\mathbb R also addresses limiting processes, not just one missing root. Complex numbers are developed in their own chapter.

A rational number has many fractional representations. For nonzero denominators,

pq=rs⟺ps=rq.\frac pq=\frac rs\quad\Longleftrightarrow\quad ps=rq.

The fractions 1/21/2 and 2/42/4 therefore represent one number, not two elements of Q\mathbb Q. A formal construction treats equivalent integer pairs as the same rational number; this is an application of equivalence classes.

The irrational numbers form R∖Q\mathbb R\setminus\mathbb Q. They are not another nested field: 2\sqrt2 and −2-\sqrt2 are irrational, but their sum is rational. Prime numbers likewise form a special subset of N\mathbb N, rather than a new number system in the inclusion chain. A prime is an integer greater than 11 whose only positive divisors are 11 and itself; in particular, 11 is not prime.

Decimal representations are not the numbers themselves

A rational number has a terminating or eventually periodic decimal expansion. In long division by a positive denominator qq, there are only qq possible remainders. A zero remainder ends the expansion; otherwise a remainder repeats and the subsequent digits repeat. Conversely, an eventually repeating tail is a geometric series, so it represents a rational number.

For instance, 1/3=0.3‾1/3=0.\overline3. Decimal expansions can also have two representations of the same value: 0.999…=10.999\ldots=1. Infinite nonperiodic decimals represent irrational numbers, but merely seeing a long finite prefix does not establish nonperiodicity. A real number can be approximated by rational decimal truncations; this is a property of the real system, not a self-contained construction of it before limits have been defined.

Cardinality: Cantor’s diagonal argument

The rationals are dense: between any two distinct rationals there is another rational. Yet they can still be listed. To prove that the reals are uncountable, we need a different obstruction: every proposed list misses a real number, regardless of how cleverly it is arranged.

TheoremThe interval (0,1) is uncountable

There is no sequence x1,x2,…x_1,x_2,\ldots whose entries include every real number in (0,1)(0,1).

The indices here start at 11 to match the decimal places. Starting at 00 instead would not affect countability. Repetitions in the proposed list are allowed: even a list with repetitions cannot cover the interval.

Step 1: suppose a complete list exists

Proof

Assume that all the reals in (0,1)(0,1) appear in a sequence. For each entry, choose the decimal expansion that is not eventually all nines. For example, use 0.5000…0.5000\ldots rather than 0.4999…0.4999\ldots.

Write

xk=0.dk1dk2dk3….x_k=0.d_{k1}d_{k2}d_{k3}\ldots.

The first subscript specifies the row; the second specifies a digit position. Thus dknd_{kn} is the nnth decimal digit of the kkth number. A finite table can display only finitely many rows and columns, but the assumed list and all its decimal expansions continue indefinitely.

Blue boxes select one diagonal digit from each row; green boxes form a new real number that differs from row n at digit n. The displayed prefixes illustrate an infinite construction.

Blue boxes select one diagonal digit from each row; green boxes form a new real number that differs from row n at digit n. The displayed prefixes illustrate an infinite construction.

The diagonal digits shown are 1,3,1,5,11,3,1,5,1; the construction changes them to 2,1,2,1,22,1,2,1,2. Only the first five positions are displayed: the rule defines every subsequent digit as well.

Step 2: change the diagonal, one digit per row

Read d11,d22,d33,…d_{11},d_{22},d_{33},\ldots: the first digit of the first number, the second digit of the second number, and so on. Define

en={1,dnn≠1,2,dnn=1.e_n= \begin{cases} 1,&d_{nn}\ne1,\\ 2,&d_{nn}=1. \end{cases}

In either case, en≠dnne_n\ne d_{nn}. The essential choice is the pairing of row nn with position nn: we reserve a specific position at which to defeat each row.

Step 3: the new digits define a real number

Set y=0.e1e2e3…y=0.e_1e_2e_3\ldots. More explicitly,

y=∑n=1∞en10n.y=\sum_{n=1}^{\infty}\frac{e_n}{10^n}.

Because each digit is 11 or 22, the partial sums are increasing and bounded. They define a real number, with

19≤y≤29.\frac19\le y\le\frac29.

These bounds follow from the geometric series with all digits equal to 11 or all equal to 22. In particular, y∈(0,1)y\in(0,1), so it belongs to the set that the list supposedly covers.

Step 4: no row can equal the new number

Choose any row kk. At position kk, the number in that row has digit dkkd_{kk}, while yy has digit eke_k. By construction,

ek≠dkk⟹y≠xk.e_k\ne d_{kk}\quad\Longrightarrow\quad y\ne x_k.

The implication uses our decimal convention: all listed expansions avoid eventual nines, and the expansion of yy, containing only ones and twos, has no terminating-decimal ambiguity. A full explanation is given below.

Since kk was arbitrary, yy differs from every number in the list. But yy is in (0,1)(0,1) and should therefore occur in a complete list. This contradiction proves that no such list exists.

Why the decimal convention matters

A difference between two digit strings does not always mean a difference between the numbers they represent. For example,

0.5000…=0.4999….0.5000\ldots=0.4999\ldots.

Changing diagonal digits to 00 or 99 without handling this issue could leave a gap in the proof. Our construction uses only 11 and 22, so its expansion is neither eventually zero nor eventually nine. The proposed list also uses the convention excluding eventual nines.

Why these are the only ambiguous decimal expansions

Suppose two decimal strings first differ at position NN, with aN<bNa_N<b_N. The advantage of the larger digit is at least 10−N10^{-N}. The entire tail can compensate by at most

∑n>N910n=10−N.\sum_{n>N}\frac9{10^n}=10^{-N}.

Equality between the represented numbers is therefore possible only if bN=aN+1b_N=a_N+1, the smaller-digit string has nothing but nines after position NN, and the larger-digit string has nothing but zeros after that position. Any smaller tail compensation leaves a strict difference.

This explains both 0.4999…=0.5000…0.4999\ldots=0.5000\ldots and why excluding eventual nines gives a unique expansion. It also shows directly that a string containing only ones and twos cannot participate in such an ambiguity.

What the argument does and does not say

The constructed number depends on the proposed list. The conclusion is “for every list, there exists a missing number,” not that one fixed number is missing from every list. If a new list includes the previous missing number, apply the same construction to that new list: it produces another missing number. An infinite list also has no final row after which a number can simply be appended.

Nor does the construction prove that its new number is irrational. Depending on the diagonal, its digits might repeat. What matters is that it is a real number outside the proposed list. Applying a similar digit-changing rule to a list of rationals does not contradict their countability, because the constructed real number need not be rational.

Since (0,1)⊆R(0,1)\subseteq\mathbb R, the real numbers are uncountable too. The irrationals are also uncountable: if they were countable, interleaving their list with a list of the rationals would make all reals countable, contradicting the result just proved.

The same “disagree with row nn at position nn” pattern appears for infinite binary sequences in Sequences and Common Sequences and for subsets of the natural numbers in Naive Set Theory. The objects change; the diagonal construction serves the same purpose.

Countability, numerical order, and available operations must be assessed separately. Adding negative numbers or rational fractions does not change the countably infinite cardinality of the natural numbers; extending to all real numbers does.

Fields, order, and completeness

A field makes arithmetic reversible where permitted

DefinitionField

A field FF has addition and multiplication mapping F×FF\times F into FF, and distinct elements 0,10,1. Addition is associative and commutative, has identity 00, and gives every element an additive inverse. Multiplication is associative and commutative, has identity 11, and gives every nonzero element a multiplicative inverse. Multiplication distributes over addition.

Subtraction means adding an additive inverse; division by a nonzero element means multiplying by its inverse. These axioms hold in Q\mathbb Q, R\mathbb R, and C\mathbb C, but not in Z\mathbb Z, where 22 has no multiplicative inverse, or in N\mathbb N, where 11 has no additive inverse.

ProofWhy zero has no inverse and cancellation needs a condition

Distributivity gives

x⋅0=x(0+0)=x⋅0+x⋅0.x\cdot0=x(0+0)=x\cdot0+x\cdot0.

Subtracting x⋅0x\cdot0 yields x⋅0=0x\cdot0=0. Therefore 0u0u cannot equal 11.

If xz=yzxz=yz and z≠0z\ne0, multiplying by z−1z^{-1} gives x=yx=y. If z=0z=0, the equality holds for all x,yx,y and provides no such conclusion. Likewise, if xy=0xy=0 and x≠0x\ne0, multiplying by x−1x^{-1} gives y=0y=0: a field has no nonzero zero divisors.

Order must be compatible with arithmetic

DefinitionOrdered field

An ordered field is a field equipped with a total order ≤\le. The order is reflexive, antisymmetric, transitive, and compares every two elements. It satisfies

x≤y⟹x+z≤y+z,x\le y\quad\Longrightarrow\quad x+z\le y+z,

and multiplying a strict inequality by a positive element preserves it:

x<y,z>0⟹xz<yz.x<y,\quad z>0\quad\Longrightarrow\quad xz<yz.

Here antisymmetry uses x≤yx\le y and y≤xy\le x to conclude x=yx=y. For the strict order, exactly one of x<yx<y, x=yx=y, and y<xy<x holds. Omitting the equality case would fail already when x=yx=y.

Multiplying by a negative number reverses inequalities. Every square is nonnegative: if x≥0x\ge0, multiply nonnegative factors; if x<0x<0, use x2=(−x)2x^2=(-x)^2 with −x>0-x>0. In particular 1>01>0, and 0<x<y0<x<y implies 0<1/y<1/x0<1/y<1/x by multiplying x<yx<y by the positive number 1/(xy)1/(xy).

The usual orders make Q\mathbb Q and R\mathbb R ordered fields. No order can make C\mathbb C an ordered field, because i2=−1i^2=-1 would be a nonnegative square, contradicting 1>01>0. The issue is compatibility with field operations, not whether complex numbers can be ordered by some other rule.

Completeness fills an order-theoretic gap

An upper bound uu for a subset SS satisfies s≤us\le u for all s∈Ss\in S. A supremum, or least upper bound, is an upper bound no larger than any other upper bound. A maximum must belong to SS; a supremum need not. For example, (0,1)(0,1) has supremum 11 and no maximum.

DefinitionCompleteness of an ordered field

An ordered field is complete if every nonempty subset that is bounded above has a supremum in that field. The real numbers form a complete ordered field.

The qualifier “in that field” matters. The rational set

S={q∈Q:q≥0, q2<2}S=\{q\in\mathbb Q:q\ge0,\ q^2<2\}

has supremum 2\sqrt2 in R\mathbb R, but no supremum in Q\mathbb Q. Rational numbers approach 2\sqrt2 from below, and every rational upper bound can be decreased while remaining above 2\sqrt2. Irrationality of 2\sqrt2 was proved in Indirect Proof. Density of rational numbers in the real line explains these approximations; one proof is an exercise below. Having a number between two others is weaker than having every required supremum. Further development of complete ordered fields is given in Lebl’s Basic Analysis [2][2] J. Lebl, Basic Analysis I: Introduction to Real Analysis, Volume I. . Online edition; Sections 1.1--1.2 on ordered fields and completeness; accessed September 6, 2026. https://www.jirka.org/ra/html/.

Completeness also implies that natural numbers are unbounded above in R\mathbb R. Otherwise, if s=sup⁡Ns=\sup\mathbb N, the smaller number s−1s-1 would not be an upper bound, so some n∈Nn\in\mathbb N would satisfy n>s−1n>s-1. Then n+1>sn+1>s, contradicting the bound. This Archimedean property ensures integers can be found beyond any fixed real threshold, as needed for floor and ceiling.

Floor, ceiling, and Euclidean remainders

DefinitionFloor and ceiling

For real xx, the floor ⌊x⌋\lfloor x\rfloor is the greatest integer at most xx, and the ceiling ⌈x⌉\lceil x\rceil is the least integer at least xx. Their defining bounds are

⌊x⌋≤x<⌊x⌋+1,\lfloor x\rfloor\le x<\lfloor x\rfloor+1,⌈x⌉−1<x≤⌈x⌉.\lceil x\rceil-1<x\le\lceil x\rceil.

The Archimedean property and the well-ordering of the nonnegative integers ensure these integers exist. Floor rounds downward, not toward zero: ⌊−2.3⌋=−3\lfloor-2.3\rfloor=-3, whereas ⌈−2.3⌉=−2\lceil-2.3\rceil=-2. At an integer kk, both equal kk.

For every integer kk, floor is constant at kk on [k,k+1)[k,k+1), while ceiling is constant at kk on (k−1,k](k-1,k]. These half-open intervals specify the endpoint values precisely.

ProofNegation exchanges floor and ceiling

Put m=⌈x⌉m=\lceil x\rceil. Its bounds give m−1<x≤mm-1<x\le m. Negating reverses the inequalities:

−m≤−x<−m+1.-m\le-x<-m+1.

Thus ⌊−x⌋=−m=−⌈x⌉\lfloor-x\rfloor=-m=-\lceil x\rceil. Replacing xx by −x-x also gives ⌈−x⌉=−⌊x⌉\lceil-x\rceil=-\lfloor x\rceil.

A positive divisor fixes the remainder convention

Let a∈Za\in\mathbb Z and m∈Z>0m\in\mathbb Z_{>0}. Set

q=⌊am⌋,r=a−mq.q=\left\lfloor\frac am\right\rfloor, \qquad r=a-mq.

The floor bounds imply mq≤a<m(q+1)mq\le a<m(q+1), hence

a=mq+r,0≤r<m.a=mq+r,\qquad 0\le r<m.

This proves existence of the Euclidean quotient and remainder. For uniqueness, two such expressions would give m(q−q′)=r′−rm(q-q')=r'-r. The right side lies strictly between −m-m and mm, whose only integer multiple of mm is zero. Therefore q=q′q=q' and r=r′r=r'.

We write a mod m=ra\bmod m=r. For example, −17=5(−4)+3-17=5(-4)+3, so −17 mod 5=3-17\bmod5=3. Divisibility means m∣am\mid a if a=mka=mk for some integer kk; for a positive mm, this is equivalent to a zero remainder. Programming-language operators and conventions for negative divisors must be checked separately; all remainder operations below use positive divisors.

Algorithms need a contract and a correctness argument

A procedure consists of precise, executable steps. To claim that it solves a problem on a specified domain, state valid inputs and required outputs, prove it terminates on every valid input, and prove the returned result meets that requirement. Correctness is not established merely because the arithmetic problem has a solution.

Pseudocode describes these steps without committing to a programming language. The symbol ←\gets means assignment; an equality in a proof states a mathematical relation. We use exact arithmetic here, so machine overflow is outside this model.

Halving and doubling multiplication

Given M∈ZM\in\mathbb Z and N∈NN\in\mathbb N, the following algorithm returns MNMN. Only the second input must be nonnegative; zero is included.

Algorithm 1 Halving and Doubling Multiplication

Require: M∈ZM \in \mathbb{Z}, N∈NN \in \mathbb{N}

Ensure: the product MNMN

1:A←MA \gets M, B←NB \gets N, p←0p \gets 0

2:while B>0B > 0 do

3:if B mod 2=1B \bmod 2 = 1 then

4:p←p+Ap \gets p+A

5:end if

6:A←2AA \gets 2A

7:B←⌊B/2⌋B \gets \lfloor B/2 \rfloor

8:end while

9:return pp

For M=73,N=41M=73,N=41, the loop visits the following values before updating them:

AABBContribution to pp
737341417373
146146202000
292292101000
58458455584584
116811682200
233623361123362336

The sum is 73+584+2336=2993=73⋅4173+584+2336=2993=73\cdot41.

ProofInvariant and termination

At the start of every iteration, the invariant is p+AB=MNp+AB=MN. It holds initially. Write the current B=2q+rB=2q+r, where r∈{0,1}r\in\{0,1\}. One full iteration changes the state to p′=p+rAp'=p+rA, A′=2AA'=2A, B′=qB'=q, giving

p′+A′B′=p+rA+2Aq=p+A(2q+r)=p+AB.\begin{aligned} p'+A'B'&=p+rA+2Aq\\ &=p+A(2q+r)\\ &=p+AB. \end{aligned}

Thus the invariant is preserved in both parity cases. While B>0B>0, its new value ⌊B/2⌋\lfloor B/2\rfloor is a strictly smaller nonnegative integer. The loop therefore terminates. At termination B=0B=0, and the invariant yields p=MNp=MN. If N=0N=0, the loop is skipped and the result is already correct.

Complexity depends on the input size and cost model

For N>0N>0, after kk completed iterations, while that state exists,

Bk=⌊N2k⌋.B_k=\left\lfloor\frac{N}{2^k}\right\rfloor.

Repeated integer halving gives this expression; it follows by dividing NN into a multiple of 2k2^k and a remainder. If 2L−1≤N<2L2^{L-1}\le N<2^L, then BL−1=1B_{L-1}=1 and BL=0B_L=0. Hence the exact iteration count is

L=⌊log⁡2N⌋+1.L=\lfloor\log_2N\rfloor+1.

This counts the iteration starting with B=1B=1. For N=0N=0, there are zero iterations and no logarithm is used. The value LL is also the binary length of a positive NN, so logarithmic dependence on the numerical value is linear dependence on that input’s bit length.

DefinitionBig-O and a tight bound

For eventually nonnegative functions T,fT,f on the natural numbers, write T(n)=O(f(n))T(n)=O(f(n)) if there exist constants c>0c>0 and n0n_0 such that

0≤T(n)≤cf(n)(n≥n0).0\le T(n)\le c f(n)\qquad(n\ge n_0).

Write T(n)=Θ(f(n))T(n)=\Theta(f(n)) when both upper and lower bounds by positive constant multiples of f(n)f(n) hold eventually.

Big-O is an asymptotic upper bound on a function, not a statement that an algorithm has been measured, nor a synonym for worst-case analysis. One may bound a worst-case, average-case, or other specified cost function. Common growth rates include 11, log⁡n\log n, nn, nlog⁡nn\log n, n2n^2, and 2n2^n.

If each exact integer operation is charged one unit, multiplication above uses Θ(L)\Theta(L) loop work for N>0N>0 and a constant number of integer registers. That does not mean constant memory in bits: those registers store larger integers as inputs grow. With ordinary binary representations and schoolbook operations, an upper bound is O(L(Mb+L))O(L(M_b+L)) bit operations and O(Mb+L)O(M_b+L) working bits, where MbM_b is the bit length of ∣M∣|M|, taking at least one bit for zero. This bound charges up to O(Mb+L)O(M_b+L) for each iteration. A sharper implementation analysis may improve it, but must still state its model.

Recursion defines objects by smaller objects

A recursive definition needs starting cases, construction rules, and a reason the recursive dependencies cannot descend forever. For instance,

0!=1,(n+1)!=(n+1)n!0!=1,\qquad (n+1)!=(n+1)n!

defines a unique value for each n∈Nn\in\mathbb N: reaching the next value uses one already defined. A recursive evaluation of n!n! reduces the nonnegative argument until it reaches zero. A formula referring to itself does not by itself guarantee existence, uniqueness, or termination.

Finite full binary trees

Define trees by exactly these rules: Leaf⁡\operatorname{Leaf} is a tree, and if L,RL,R are trees, then Node⁡(L,R)\operatorname{Node}(L,R) is a tree. Nothing else is a tree. Here constructors are distinct and their arguments are ordered, so every non-leaf has unique left and right subtrees. These are finite full binary trees, built using finitely many constructor applications; every internal node has exactly two children.

Define the number of leaves ℓ\ell and internal nodes ii recursively:

ℓ(Leaf⁡)=1,i(Leaf⁡)=0,\ell(\operatorname{Leaf})=1,\qquad i(\operatorname{Leaf})=0, ℓ(Node⁡(L,R))=ℓ(L)+ℓ(R),\ell(\operatorname{Node}(L,R))=\ell(L)+\ell(R), i(Node⁡(L,R))=1+i(L)+i(R).i(\operatorname{Node}(L,R))=1+i(L)+i(R).

This gives a recursive algorithm for counting leaves:

Algorithm 2 Count Leaves

Require: a finite full binary tree TT

Ensure: the number of leaves in TT

1:if T=Leaf⁡T=\operatorname{Leaf} then

2:return 11

3:else

4:let T=Node⁡(L,R)T=\operatorname{Node}(L,R)

5:a←a \gets CountLeaves(LL)

6:b←b \gets CountLeaves(RR)

7:return a+ba+b

8:end if

Each call uses a proper subtree with fewer nodes, so recursion terminates on the specified finite inputs. This justification would not automatically apply to infinite trees.

Structural induction follows the construction rules

To prove a property for every tree, prove it for Leaf⁡\operatorname{Leaf}, then assume it for arbitrary L,RL,R and prove it for Node⁡(L,R)\operatorname{Node}(L,R). These cases cover every permitted finite construction. This is structural induction; induction on natural numbers is the corresponding pattern for zero and successor.

ProofLeaves are one more than internal nodes

For a leaf, ℓ=1=i+1\ell=1=i+1. Suppose ℓ(L)=i(L)+1\ell(L)=i(L)+1 and ℓ(R)=i(R)+1\ell(R)=i(R)+1, and put T=Node⁡(L,R)T=\operatorname{Node}(L,R). Then

ℓ(T)=ℓ(L)+ℓ(R)=i(L)+i(R)+2=i(T)+1.\begin{aligned} \ell(T)&=\ell(L)+\ell(R)\\ &=i(L)+i(R)+2\\ &=i(T)+1. \end{aligned}

Thus every finite full binary tree has exactly one more leaf than internal nodes. The hypotheses concern both immediate subtrees, rather than an unspecified “previous tree.”

Exercises

ExerciseCountable size, supremum, and maximum

For S={1−1/n:n∈Z>0}S=\{1-1/n:n\in\mathbb Z_{>0}\}, determine its cardinality, supremum, and whether it has a maximum.

Show solution
Solution

The listed values are strictly increasing, giving a bijection from positive integers onto SS, so SS is countably infinite. Every value is less than 11. If u<1u<1, choose an integer n>1/(1−u)n>1/(1-u) using the Archimedean property; then 1−1/n>u1-1/n>u. Thus no number below 11 is an upper bound, and sup⁡S=1\sup S=1. It is not a maximum because 1∉S1\notin S; also the next listed value always exceeds the current one.

ExerciseFind a rational between two reals

Let a<ba<b be real. Use the Archimedean property and floor to find integers pp and n>0n>0 such that a<p/n<ba<p/n<b.

Show solution
Solution

Choose a positive integer nn with n(b−a)>1n(b-a)>1, then put p=⌊na⌋+1p=\lfloor na\rfloor+1. The floor bounds give

na<p≤na+1<nb.na<p\le na+1<nb.

Dividing by positive nn proves the claim. This establishes density in the real line without implying uncountability of Q\mathbb Q.

ExerciseTranslate floor comparisons into real inequalities

For integer mm, characterize each condition: ⌊x⌋<m\lfloor x\rfloor<m, m≤⌊x⌋m\le\lfloor x\rfloor, ⌊x⌋≤m\lfloor x\rfloor\le m, m<⌊x⌋m<\lfloor x\rfloor, ⌊x⌋=m\lfloor x\rfloor=m, and ⌈x⌉=m\lceil x\rceil=m.

Show solution
Solution

Their equivalent conditions, in the same order, are

x<m,m≤x,x<m+1,m+1≤x,m≤x<m+1,m−1<x≤m.\begin{gathered} x<m,\qquad m\le x,\\ x<m+1,\qquad m+1\le x,\\ m\le x<m+1,\\ m-1<x\le m. \end{gathered}

The last two are the defining intervals. The second says that integer mm is a candidate at or below xx, so it is no greater than the greatest such integer; the first is its negation. For the third and fourth, apply these facts with integer m+1m+1 and use that no integer lies strictly between mm and m+1m+1.

ExerciseParity and negative integer inputs

Prove both identities for every integer kk:

⌈k−12⌉=⌊k2⌋,\left\lceil\frac{k-1}{2}\right\rceil=\left\lfloor\frac k2\right\rfloor,⌊k−12⌋=⌈k2⌉−1.\left\lfloor\frac{k-1}{2}\right\rfloor=\left\lceil\frac k2\right\rceil-1.
Show solution
Solution

If k=2tk=2t, the two sides of the first equality are tt, and those of the second are t−1t-1. If k=2t+1k=2t+1, both sides of each equality are tt. Every integer has one of these forms, including negative integers. For example, k=−3k=-3 gives −2-2 on both sides of both equalities.

ExerciseRemainders below zero

Find the Euclidean quotient and remainder of −23-23 divided by 77. Why is truncating −23/7-23/7 toward zero not the required quotient?

Show solution
Solution

The quotient is −4-4 and the remainder is 55, since −23=7(−4)+5-23=7(-4)+5 with 0≤5<70\le5<7. Truncation gives −3-3 and hence a remainder of −2-2, outside the specified range. The sign convention belongs to the definition, rather than being an implementation detail one can ignore.

ExerciseA field with three elements

Let F={0,1,u}F=\{0,1,u\} be a field with three distinct elements. Show that u+u=1u+u=1 and u2=1u^2=1, and construct its operation tables.

Show solution
Solution

By cancellation, 1+1≠11+1\ne1. If 1+1=01+1=0, then u+1u+1 could be neither uu nor 11 by cancellation, nor 00 because u+1=1+1u+1=1+1 would give u=1u=1. There would be no possible value in FF. Thus 1+1=u1+1=u. Next, 1+u1+u is neither 11 nor uu, so it is 00. Associativity gives

u+u=(1+1)+u=1+(1+u)=1.\begin{aligned} u+u&=(1+1)+u\\ &=1+(1+u)=1. \end{aligned}

Also u2≠0u^2\ne0 because a field has no nonzero zero divisors, and u2≠uu^2\ne u because multiplying by u−1u^{-1} would give u=1u=1. Hence u2=1u^2=1. The tables are

+01u001u11u0uu01\begin{array}{c|ccc} +&0&1&u\\\hline 0&0&1&u\\ 1&1&u&0\\ u&u&0&1 \end{array}⋅01u0000101uu0u1\begin{array}{c|ccc} \cdot&0&1&u\\\hline 0&0&0&0\\ 1&0&1&u\\ u&0&u&1 \end{array}

This is arithmetic modulo 33, with uu representing 22. The additive inverse of 11 is uu, and the additive inverse of uu is 11; inverse relationships are mutual.

ExerciseExploration: four-element and six-element fields

Can fields have exactly four or exactly six elements? Distinguish a four-element field from arithmetic modulo 44.

Discussion guide
Solution

A four-element field exists with elements 0,1,α,α+10,1,\alpha,\alpha+1. Compute polynomial coefficients modulo 22 and reduce using α2=α+1\alpha^2=\alpha+1. Formally, this is the polynomial quotient by t2+t+1t^2+t+1 over the two-element field. The inherited operations satisfy the ring laws, and its nonzero elements have inverses: 1−1=11^{-1}=1 and α(α+1)=1\alpha(\alpha+1)=1. In contrast, modulo 44, the nonzero element 22 has square zero, so that arithmetic is not a field.

A finite field has prime characteristic pp: the smallest positive number of copies of 11 summing to zero cannot be composite, since a factorization would give nonzero zero divisors. It contains a copy of the pp-element field. As a vector space over that subfield it has some finite dimension dd, so counting coefficient choices gives exactly pdp^d elements. Six is not a prime power, so a six-element field cannot exist. This argument previews polynomial quotients and vector spaces; those constructions belong to Abstract Algebra.

ExerciseSquare-and-multiply with an invariant

Assume an associative multiplication with identity 11, and let n∈Nn\in\mathbb N. Prove that the following algorithm returns bnb^n. Powers of exponent zero mean the identity.

Algorithm 3 Square-and-Multiply

Require: n∈Nn \in \mathbb{N} and an element bb

Ensure: bnb^n

1:p←1p \gets 1, s←bs \gets b, a←na \gets n

2:while a>0a > 0 do

3:if a mod 2=1a \bmod 2 = 1 then

4:p←psp \gets p s

5:end if

6:s←s2s \gets s^2

7:a←⌊a/2⌋a \gets \lfloor a/2 \rfloor

8:end while

9:return pp

Show solution
Solution

Use psa=bnps^a=b^n as the invariant. Initially it holds. Write the current a=2q+ra=2q+r with r∈{0,1}r\in\{0,1\}. The updated values are p′=psrp'=ps^r, s′=s2s'=s^2, and a′=qa'=q, so

p′(s′)a′=(psr)(s2)q=psr+2q=psa.\begin{aligned} p'(s')^{a'}&=(ps^r)(s^2)^q\\ &=ps^{r+2q}\\ &=ps^a. \end{aligned}

Associativity and laws for powers of the same element suffice; arbitrary elements need not commute. Positive aa strictly decreases, so the loop terminates. At a=0a=0 the invariant says p=bnp=b^n. For n=0n=0 it returns the identity without entering the loop. For positive nn the number of iterations is ⌊log⁡2n⌋+1\lfloor\log_2n\rfloor+1, as in multiplication. Any lower bound asserting ak>0a_k>0 applies only before termination, not for every nonnegative kk.

ExerciseStructural induction with height

Give a leaf height zero and define a node’s height as one plus the maximum of its two subtree heights. Prove that a finite full binary tree of height hh has at most 2h2^h leaves.

Show solution
Solution

A leaf has 1=201=2^0 leaves. At a node of height hh, both subtree heights are at most h−1h-1. By the two induction hypotheses, each subtree has at most 2h−12^{h-1} leaves, so together they have at most 2h2^h. This proves the claim for every finite constructor-built tree; no assumption that the two subtrees have equal height is needed.

ExerciseName the resource being counted

An implementation uses three integer variables and makes LL loop iterations. Does that alone prove O(1)O(1) bit space and O(L)O(L) bit time?

Show solution
Solution

No. A constant number of variables gives a constant register count, but their values may require an increasing number of bits. Similarly, LL iterations imply O(L)O(L) time only when each iteration has bounded cost in the stated model. Exact arithmetic on growing integers must account for operand lengths. Identify both input encoding and operation cost before reporting a complexity bound.

References

  1. [1] E. Lehman, F. T. Leighton, and A. R. Meyer, Mathematics for Computer Science. MIT OpenCourseWare, 2015. Revised May 18, 2015; infinite sets, recursive data, and invariants. https://ocw.mit.edu/courses/6-042j-mathematics-for-computer-science-spring-2015/mit6_042js15_textbook.pdf ↩
  2. [2] J. Lebl, Basic Analysis I: Introduction to Real Analysis, Volume I. . Online edition; Sections 1.1--1.2 on ordered fields and completeness; accessed September 6, 2026. https://www.jirka.org/ra/html/ ↩