taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Volume P2 · Concrete mathematics

Floors, ceilings, logarithms and mod

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Contents
  1. Powers and logarithms
  2. Floors and ceilings
  3. The mod operation
  4. Sums of floors

Powers and logarithms

Statement

This section defines bxb^x for a real number b>0b > 0 and a real exponent xx. It does so in three steps. Integer exponents need only the field operations. Rational exponents need roots, and roots need the completeness of the real numbers: each nonempty set of reals with an upper bound has a least upper bound. Real exponents need a limit. At each step the new definition agrees with the old one where both apply, and the laws of exponents stay true. The logarithm is the inverse function of x↦bxx \mapsto b^x.

Definition P2.4.1 (integer powers). Let bb be a number and nn an integer. For n≥0n \ge 0 the power bnb^n is 11 when n=0n = 0 and bn−1⋅bb^{n-1} \cdot b when n>0n > 0. For n<0n < 0 and b≠0b \ne 0 the power is bn=1/b−nb^n = 1/b^{-n}. The number bb is the base and nn is the exponent. In particular 00=10^0 = 1.

Lean: Mathlib b ^ n for n : ℕ (Monoid.npow, with pow_zero and pow_succ) and for n : ℤ (zpow, with zpow_neg) (proved in Mathlib).

Example P2.4.2. 2−3=1/23=182^{-3} = 1/2^3 = \frac{1}{8}, (−3)3=(−3)(−3)(−3)=−27(-3)^3 = (-3)(-3)(-3) = -27, and (12)−4=1/116=16\bigl(\frac{1}{2}\bigr)^{-4} = 1/\frac{1}{16} = 16. A negative base with an integer exponent is no problem: the sign of (−3)n(-3)^n is (−1)n(-1)^n.

Example P2.4.3. The value 00=10^0 = 1 is a convention, and it is the convention that keeps formulas short. With it, a polynomial ∑0≤k≤nakxk\sum_{0 \le k \le n} a_k x^k has the value a0a_0 at x=0x = 0, and the binomial theorem (x+y)n=∑k(nk)xkyn−k(x + y)^n = \sum_k \binom{n}{k} x^k y^{n-k} holds also for x=0x = 0. The power 0n0^n for n<0n < 0 has no value.

Theorem P2.4.4 (laws of exponents). Let bb and cc be numbers and let mm and nn be integers. Then

bm+n=bmbn,(bm)n=bmn,(bc)n=bncn,b^{m+n} = b^m b^n, \qquad (b^m)^n = b^{mn}, \qquad (bc)^n = b^n c^n,

for all b,cb, c when m,n≥0m, n \ge 0, and for b,c≠0b, c \ne 0 when mm or nn is negative.

Lean: Mathlib pow_add, pow_mul and mul_pow for natural exponents, and zpow_add₀, zpow_mul and mul_zpow for integer exponents and a nonzero base (proved in Mathlib).

Proof. First let m,n≥0m, n \ge 0. The first law holds for n=0n = 0, and bm+n+1=bm+nb=bmbnb=bmbn+1b^{m+n+1} = b^{m+n} b = b^m b^n b = b^m b^{n+1}, so it holds for all nn by induction. The second law follows from the first by induction on nn: (bm)n+1=bmnbm=bmn+m(b^m)^{n+1} = b^{mn} b^m = b^{mn + m}. The third law uses the commutative law: (bc)n+1=bncnbc=bn+1cn+1(bc)^{n+1} = b^n c^n bc = b^{n+1} c^{n+1}. Now let b,c≠0b, c \ne 0. The definition gives b−n=1/bnb^{-n} = 1/b^n for every integer nn, also for n<0n < 0. For the first law with m≥0>nm \ge 0 > n and m+n≥0m + n \ge 0, the natural case gives bm+nb−n=bmb^{m+n} b^{-n} = b^m, so bm+n=bm/b−n=bmbnb^{m+n} = b^m / b^{-n} = b^m b^n. The other cases of signs reduce to the natural case in the same way, by moving the negative powers to the other side. The second and third laws for negative exponents follow from 1/(xy)=(1/x)(1/y)1/(xy) = (1/x)(1/y). □\square

Definition P2.4.5 (rational powers). Let b>0b > 0 be real and let r=p/qr = p/q be rational with integers pp and q≥1q \ge 1. The power bp/qb^{p/q} is the unique real y>0y > 0 with yq=bpy^q = b^p. In particular b1/qb^{1/q} is the positive qqth root of bb.

Lean: Mathlib defines b ^ x for real b and x (Real.rpow); Real.rpow_intCast and Real.rpow_natCast show that it agrees with the integer power, and Real.pow_rpow_inv_natCast shows that b1/qb^{1/q} is the qqth root (proved in Mathlib).

Example P2.4.6. 82/3=48^{2/3} = 4, because 4>04 > 0 and 43=64=824^3 = 64 = 8^2. Also 27−1/3=1327^{-1/3} = \frac{1}{3}, because (13)3=127=27−1\bigl(\frac{1}{3}\bigr)^3 = \frac{1}{27} = 27^{-1}. The fraction need not be in lowest terms: 84/68^{4/6} is the positive yy with y6=84=4096y^6 = 8^4 = 4096, and this is again 44.

Example P2.4.7. The definition needs b>0b > 0. For b=−8b = -8 the fraction 13\frac{1}{3} suggests the value −2-2, since (−2)3=−8(-2)^3 = -8. The fraction 26\frac{2}{6} has the same value, and it asks for yy with y6=(−8)2=64y^6 = (-8)^2 = 64, which gives y=2y = 2 for y>0y > 0 or y=−2y = -2. No single choice gives a function of the rational number rr that keeps all the laws of exponents.

Theorem P2.4.8 (laws of rational exponents). The power bp/qb^{p/q} is well defined: there is exactly one y>0y > 0 with yq=bpy^q = b^p, and yy depends only on the number p/qp/q. For b,c>0b, c > 0 and rational rr and ss,

br+s=brbs,(br)s=brs,(bc)r=brcr.b^{r+s} = b^r b^s, \qquad (b^r)^s = b^{rs}, \qquad (bc)^r = b^r c^r.

For b>1b > 1 and rational r<sr < s, br<bsb^r < b^s.

Lean: special cases of Real.rpow_add, Real.rpow_mul and Real.mul_rpow; monotonicity is Real.rpow_lt_rpow_left_iff (proved in Mathlib).

Proof. For y,z>0y, z > 0 and q≥1q \ge 1, y<zy < z implies yq<zqy^q < z^q, by induction on qq. So there is at most one y>0y > 0 with yq=ay^q = a for a given a>0a > 0. For existence, let S={y>0:yq≤a}S = \{y > 0 : y^q \le a\}. The set contains min⁡(1,a)\min(1, a) and has the upper bound max⁡(1,a)\max(1, a), so it has a least upper bound y0>0y_0 > 0. If y0q<ay_0^q < a, a number slightly larger than y0y_0 is still in SS; if y0q>ay_0^q > a, a number slightly smaller is still an upper bound. Both contradict the choice of y0y_0, so y0q=ay_0^q = a. (The words "slightly larger" use the bound (y+h)q≤yq+h⋅q(y+1)q−1(y + h)^q \le y^q + h \cdot q(y+1)^{q-1} for 0<h≤10 < h \le 1, which follows from the binomial theorem.) Now let p/q=p′/q′p/q = p'/q', so pq′=p′qpq' = p'q. If yq=bpy^q = b^p and zq′=bp′z^{q'} = b^{p'}, then yqq′=bpq′=bp′q=zqq′y^{qq'} = b^{pq'} = b^{p'q} = z^{qq'}, so y=zy = z. So the power depends only on the number rr.

For the laws, write r=p/qr = p/q and s=p′/qs = p'/q with one denominator. Then (brbs)q=bpbp′=bp+p′(b^r b^s)^q = b^p b^{p'} = b^{p+p'} by the integer laws, so brbsb^r b^s is the positive qqth root of bp+p′b^{p+p'}, which is br+sb^{r+s}. The other two laws follow in the same way: take the power with the exponent equal to the product of the denominators, and use the integer laws and the uniqueness of positive roots. For monotonicity, let b>1b > 1. Then b1/q>1b^{1/q} > 1, since a positive y≤1y \le 1 has yq≤1<by^q \le 1 < b. So bs−r=(b1/q)p′−p>1b^{s-r} = (b^{1/q})^{p' - p} > 1 when p′>pp' > p, and bs=brbs−r>brb^s = b^r b^{s-r} > b^r. □\square

Definition P2.4.9 (real powers). Let b>1b > 1 and let xx be real. The power bxb^x is the least upper bound of the set {br:r rational,r≤x}\{b^r : r \text{ rational}, r \le x\}. For b=1b = 1 let 1x=11^x = 1, and for 0<b<10 < b < 1 let bx=(1/b)−xb^x = (1/b)^{-x}.

Lean: Real.rpow, with Real.rpow_def_of_pos (bx=exln⁡bb^x = e^{x \ln b} for b>0b > 0, proved in Mathlib). Mathlib defines the power through the exponential function and not through a least upper bound. The two definitions agree, because both are continuous in xx and both agree with the rational power; this agreement is not yet stated in Lean.

Example P2.4.10. The set has an upper bound: if nn is an integer with n≥xn \ge x, then br≤bnb^r \le b^n for each rational r≤xr \le x. For rational xx the least upper bound is bxb^x itself, by the monotonicity in the theorem above, so the new definition agrees with the old one. For x=2x = \sqrt{2}, the rationals 1.414<2<1.4151.414 < \sqrt{2} < 1.415 give 2.66475<22<2.666602.66475 < 2^{\sqrt{2}} < 2.66660, to five decimal places. The true value is 2.665142.66514.

Example P2.4.11. The rationals 1.584961.58496 and 1.584971.58497 give 21.58496=2.9999952^{1.58496} = 2.999995 and 21.58497=3.0000162^{1.58497} = 3.000016, to six decimal places. Since x↦2xx \mapsto 2^x is increasing, the real number xx with 2x=32^x = 3 lies between 1.584961.58496 and 1.584971.58497. This number is lg⁡3\lg 3, defined below.

Theorem P2.4.12 (laws of real exponents). Let b,c>0b, c > 0 and let xx and yy be real. Then bx+y=bxbyb^{x+y} = b^x b^y, (bx)y=bxy(b^x)^y = b^{xy} and (bc)x=bxcx(bc)^x = b^x c^x. For b>1b > 1 the function x↦bxx \mapsto b^x is strictly increasing and continuous, it tends to 00 as x→−∞x \to -\infty and to ∞\infty as x→∞x \to \infty, and so it is a bijection from ℝ\mathbb{R} onto the positive reals.

Lean: Real.rpow_add, Real.rpow_mul, Real.mul_rpow, Real.strictMono_rpow_of_base_gt_one and Real.continuous_const_rpow (proved in Mathlib).

Proof. Let b>1b > 1. The key fact is that b1/n→1b^{1/n} \to 1 as n→∞n \to \infty. Write b1/n=1+tnb^{1/n} = 1 + t_n with tn>0t_n > 0. Bernoulli's inequality (1+t)n≥1+nt(1 + t)^n \ge 1 + nt for t≥0t \ge 0, which follows by induction on nn, gives b≥1+ntnb \ge 1 + n t_n, so 0<tn≤(b−1)/n0 < t_n \le (b - 1)/n.

Let xx be real and let r≤x≤sr \le x \le s be rationals with s−r≤1/ns - r \le 1/n. Then 0≤bs−br=br(bs−r−1)≤bstn0 \le b^s - b^r = b^r (b^{s-r} - 1) \le b^s t_n. So the least upper bound of the brb^r with r≤xr \le x equals the greatest lower bound of the bsb^s with s≥xs \ge x. The product law follows: for rationals r≤xr \le x and r′≤yr' \le y, brbr′=br+r′≤bx+yb^r b^{r'} = b^{r+r'} \le b^{x+y}, so bxby≤bx+yb^x b^y \le b^{x+y}; the same argument with rationals above xx and yy gives bxby≥bx+yb^x b^y \ge b^{x+y}. For x<yx < y, choose rationals x<r<s<yx < r < s < y; then bx≤br<bs≤byb^x \le b^r < b^s \le b^y. For continuity at xx, ∣bx+h−bx∣=bx∣bh−1∣|b^{x+h} - b^x| = b^x |b^h - 1|, and for ∣h∣≤1/n|h| \le 1/n this is at most bxmax⁡(tn,1−b−1/n)≤bxtnb^x \max(t_n, 1 - b^{-1/n}) \le b^x t_n, which tends to 00. For the limits, bn=(1+(b−1))n≥1+n(b−1)→∞b^n = (1 + (b-1))^n \ge 1 + n(b-1) \to \infty, and b−n=1/bn→0b^{-n} = 1/b^n \to 0. The intermediate value theorem now shows that each y>0y > 0 is a value bxb^x. The law (bx)y=bxy(b^x)^y = b^{xy} holds for rational yy by the rational laws and continuity in xx; for real yy it follows by continuity in yy. The law for (bc)x(bc)^x follows in the same way. The cases b=1b = 1 and b<1b < 1 reduce to b>1b > 1 by the definition. □\square

Definition P2.4.13 (logarithm). Let b>0b > 0 with b≠1b \ne 1, and let y>0y > 0. The logarithm of yy to the base bb is the unique real number x=log⁡byx = \log_b y with bx=yb^x = y.

Lean: Real.logb b y, defined as ln⁡y/ln⁡b\ln y / \ln b; Real.rpow_logb states blog⁡by=yb^{\log_b y} = y (proved in Mathlib).

Example P2.4.14. log⁡21024=10\log_2 1024 = 10, log⁡100.001=−3\log_{10} 0.001 = -3, and log⁡1/28=−3\log_{1/2} 8 = -3 because (12)−3=8\bigl(\frac{1}{2}\bigr)^{-3} = 8. A logarithm is an exponent: the question "log⁡by=?\log_b y = ?" is the question "to which power must bb be raised to give yy?".

Example P2.4.15. From 210=1024>1032^{10} = 1024 > 10^3 it follows that 10log⁡102>310 \log_{10} 2 > 3, so log⁡102>0.3\log_{10} 2 > 0.3. In the same way 213=8192<1042^{13} = 8192 < 10^4 gives log⁡102<413=0.3077\log_{10} 2 < \frac{4}{13} = 0.3077. To five places, log⁡102=0.30103\log_{10} 2 = 0.30103.

Theorem P2.4.16 (laws of logarithms). Let b>0b > 0 with b≠1b \ne 1, let x,y>0x, y > 0 and let cc be real. Then

log⁡b(xy)=log⁡bx+log⁡by,log⁡b(xc)=clog⁡bx,log⁡b1=0,log⁡bb=1.\log_b (xy) = \log_b x + \log_b y, \qquad \log_b (x^c) = c \log_b x, \qquad \log_b 1 = 0, \qquad \log_b b = 1.

For b>1b > 1 the function log⁡b\log_b is strictly increasing and continuous on the positive reals; for 0<b<10 < b < 1 it is strictly decreasing.

Lean: Real.logb_mul, Real.logb_rpow, Real.logb_self_eq_one, Real.logb_lt_logb_iff, and for the natural logarithm Real.log_mul, Real.log_rpow and Real.log_pow (proved in Mathlib).

Proof. Let u=log⁡bxu = \log_b x and v=log⁡byv = \log_b y. Then bu+v=bubv=xyb^{u+v} = b^u b^v = xy, so log⁡b(xy)=u+v\log_b(xy) = u + v. Also bcu=(bu)c=xcb^{cu} = (b^u)^c = x^c, so log⁡b(xc)=cu\log_b(x^c) = cu. The values at 11 and bb come from b0=1b^0 = 1 and b1=bb^1 = b. The inverse of a strictly increasing continuous bijection from ℝ\mathbb{R} onto the positive reals is strictly increasing and continuous. For b<1b < 1, log⁡by=−log⁡1/by\log_b y = -\log_{1/b} y. □\square

Definition P2.4.17 (binary and natural logarithms). The binary logarithm is lg⁡x=log⁡2x\lg x = \log_2 x. The number ee is ∑k≥01/k!=2.71828 18284 59…\sum_{k \ge 0} 1/k! = 2.71828\,18284\,59\ldots, which is also the limit of (1+1/n)n(1 + 1/n)^n. The natural logarithm is ln⁡x=log⁡ex\ln x = \log_e x. The common logarithm is log⁡10x\log_{10} x.

Lean: Real.log is the natural logarithm, and Real.logb 2 is the binary logarithm; Real.exp 1 is ee, with the bounds Real.exp_one_gt_d9 and Real.exp_one_lt_d9 (proved in Mathlib).

Example P2.4.18. lg⁡x\lg x counts the halvings: lg⁡1024=10\lg 1024 = 10, and lg⁡106=19.93\lg 10^6 = 19.93, so a million can be halved 1919 times and stay at least 11, and the 2020th halving takes it below 11. Binary search in a sorted table of a million entries needs about 2020 comparisons for this reason.

Example P2.4.19. ln⁡2=0.69315\ln 2 = 0.69315 and ln⁡10=2.30259\ln 10 = 2.30259, to five places. The natural logarithm is the one with the simplest derivative, ddxln⁡x=1x\frac{d}{dx} \ln x = \frac{1}{x}, and its value ln⁡n\ln n is close to the harmonic number HnH_n of Chapter 3.

Theorem P2.4.20 (change of base). Let b,c>0b, c > 0 with b,c≠1b, c \ne 1, and let x>0x > 0. Then

log⁡cx=log⁡bxlog⁡bc.\log_c x = \frac{\log_b x}{\log_b c}.

In particular lg⁡x=ln⁡x/ln⁡2\lg x = \ln x / \ln 2, and log⁡bc⋅log⁡cb=1\log_b c \cdot \log_c b = 1.

Lean: Real.log_div_log (ln⁡x/ln⁡b=log⁡bx\ln x / \ln b = \log_b x) (proved in Mathlib).

Proof. Let u=log⁡cxu = \log_c x and w=log⁡bcw = \log_b c. Then x=cu=(bw)u=bwux = c^u = (b^w)^u = b^{wu}, so log⁡bx=wu\log_b x = wu, and u=log⁡bx/wu = \log_b x / w. Here w≠0w \ne 0 because c≠1c \ne 1. For the last claim take x=bx = b. □\square

Theorem P2.4.21 (number of digits). Let b≥2b \ge 2 and n≥1n \ge 1 be integers. The representation of nn in radix bb with a nonzero leading digit has exactly dd digits, where dd is the unique integer with bd−1≤n<bdb^{d-1} \le n < b^d. So d=⌊log⁡bn⌋+1=⌈log⁡b(n+1)⌉d = \lfloor \log_b n \rfloor + 1 = \lceil \log_b (n+1) \rceil, where ⌊x⌋\lfloor x \rfloor is the largest integer ≤x\le x and ⌈x⌉\lceil x \rceil is the smallest integer ≥x\ge x (Section 4.2).

Lean: Nat.length_digits states (b.digits n).length = Nat.log b n + 1 for b>1b > 1 and n≠0n \ne 0; Nat.log b n is ⌊log⁡bn⌋\lfloor \log_b n \rfloor, with Nat.pow_log_le_self and Nat.lt_pow_succ_log_self, and Real.floor_logb_natCast connects Real.logb to the integer logarithm (proved in Mathlib).

Proof. A number with the dd digits ad−1…a1a0a_{d-1} \ldots a_1 a_0 in radix bb is ∑0≤j<dajbj\sum_{0 \le j < d} a_j b^j with 0≤aj<b0 \le a_j < b. If ad−1≥1a_{d-1} \ge 1, it is at least bd−1b^{d-1}. It is at most ∑0≤j<d(b−1)bj=bd−1<bd\sum_{0 \le j < d} (b-1) b^j = b^d - 1 < b^d. So bd−1≤n<bdb^{d-1} \le n < b^d, and there is only one such dd because the intervals [bd−1,bd)[b^{d-1}, b^d) are disjoint. Take logarithms: d−1≤log⁡bn<dd - 1 \le \log_b n < d, which says d−1=⌊log⁡bn⌋d - 1 = \lfloor \log_b n \rfloor. For the ceiling, n<bdn < b^d means n+1≤bdn + 1 \le b^d, and bd−1≤nb^{d-1} \le n means bd−1<n+1b^{d-1} < n + 1. So d−1<log⁡b(n+1)≤dd - 1 < \log_b(n+1) \le d, which says d=⌈log⁡b(n+1)⌉d = \lceil \log_b(n+1) \rceil. □\square

Proof

The proofs of this section follow the order of the definitions, and the order matters. Each law for real exponents is reduced to the law for rational exponents, which is reduced to the law for integer exponents, which is a matter of induction. Two facts about the real numbers do the work between the steps. Completeness gives the roots, and so the rational powers. The estimate b1/n−1≤(b−1)/nb^{1/n} - 1 \le (b-1)/n, from Bernoulli's inequality, shows that the rational powers below xx and the rational powers above xx squeeze one number between them, and so gives the real powers with their continuity.

A shorter route, the one Mathlib takes, defines ln⁡\ln first as an integral or as the inverse of the exponential series, and then sets bx=exln⁡bb^x = e^{x \ln b}. The laws of exponents then follow from the single law eu+v=eueve^{u+v} = e^u e^v. The route of this section needs no series and no integrals, and it shows why bxb^x for irrational xx is the number that a computer approximates when it uses rational exponents.

Examples

Example P2.4.22. The binary digits of lg⁡3\lg 3 come from repeated squaring. Since 3=2⋅1.53 = 2 \cdot 1.5, lg⁡3=1+lg⁡1.5\lg 3 = 1 + \lg 1.5, and lg⁡1.5\lg 1.5 lies in [0,1)[0, 1). For a number yy in [1,2)[1, 2) write lg⁡y=0.c1c2c3…\lg y = 0.c_1 c_2 c_3 \ldots in binary. Then lg⁡(y2)=2lg⁡y=c1.c2c3…\lg(y^2) = 2 \lg y = c_1 . c_2 c_3 \ldots, so c1=1c_1 = 1 exactly when y2≥2y^2 \ge 2. In that case replace yy by y2/2y^2/2, whose binary logarithm is 0.c2c3…0.c_2 c_3 \ldots; otherwise replace yy by y2y^2. Start with y=1.5y = 1.5:

step y2y^2 bit new yy
1 2.25002.2500 11 1.12501.1250
2 1.26561.2656 00 1.26561.2656
3 1.60181.6018 00 1.60181.6018
4 2.56582.5658 11 1.28291.2829
5 1.64581.6458 00 1.64581.6458
6 2.70872.7087 11 1.35431.3543
7 1.83431.8343 00 1.83431.8343
8 3.36453.3645 11 1.68231.6823

So lg⁡1.5=0.10010101…2\lg 1.5 = 0.10010101\ldots_2, and the eight bits give 12+116+164+1256=0.58203\frac{1}{2} + \frac{1}{16} + \frac{1}{64} + \frac{1}{256} = 0.58203. The true value is lg⁡1.5=0.58496\lg 1.5 = 0.58496, and the next bits 1,1,0,0,…1, 1, 0, 0, \ldots close the gap. Each step costs one multiplication, and each step gives one bit. The rounding errors of the squarings grow, so a real program carries a few extra digits. This is the method of equations 1.2.2(16) to 1.2.2(19).

Example P2.4.23. The number 31003^{100} has ⌊100log⁡103⌋+1\lfloor 100 \log_{10} 3 \rfloor + 1 decimal digits. With log⁡103=0.4771213\log_{10} 3 = 0.4771213, the product is 47.7121347.71213, so 31003^{100} has 4848 digits. Its leading digit comes from the fractional part: 3100=1047⋅100.712133^{100} = 10^{47} \cdot 10^{0.71213} and 100.71213=5.1510^{0.71213} = 5.15, so the leading digit is 55. Indeed 3100=5153775207320113310364611297656212727021075220013^{100} = 515377520732011331036461129765621272702107522001.

Example P2.4.24. To solve 2x=10002^x = 1000, take logarithms: x=lg⁡1000=3/log⁡102=9.96578x = \lg 1000 = 3 / \log_{10} 2 = 9.96578. So 29=512<1000<1024=2102^9 = 512 < 1000 < 1024 = 2^{10}, and 10001000 has ⌊9.96578⌋+1=10\lfloor 9.96578 \rfloor + 1 = 10 binary digits: 1000=111110100021000 = 1111101000_2. The constant 1/log⁡102=lg⁡10=3.321931/\log_{10} 2 = \lg 10 = 3.32193 converts decimal digits to bits: a number with kk decimal digits has about 3.32k3.32 k bits.

Example P2.4.25. The change of base shows that all logarithms are proportional. For each b>1b > 1, log⁡bn=ln⁡n/ln⁡b\log_b n = \ln n / \ln b. So a running time of 5lg⁡n5 \lg n steps is the same as 7.213ln⁡n7.213 \ln n steps, and statements about the growth of a logarithm, such as log⁡bn/n→0\log_b n / n \to 0, do not depend on the base. This is the reason that the base of a logarithm is often left out inside OO-notation.

Example P2.4.26. The laws of exponents fail for a negative base with rational exponents, as the example after the definition of rational powers shows. They also fail for complex exponents with a careless choice of branch: the formula (bx)y=bxy(b^x)^y = b^{xy} with b=e2πib = e^{2\pi i}, x=1x = 1 and y=12y = \frac{1}{2} suggests 1=11/2=eπi=−11 = 1^{1/2} = e^{\pi i} = -1. Within this section all bases are positive and all exponents real, and no such trouble arises.

Exercises

  1. [10] Find 163/416^{3/4}, 27−2/327^{-2/3}, log⁡3181\log_3 \frac{1}{81} and log⁡432\log_4 32.

  2. [12] Let b>0b > 0, b≠1b \ne 1, x>0x > 0 and let k≠0k \ne 0 be real. Prove that log⁡bkx=1klog⁡bx\log_{b^k} x = \frac{1}{k} \log_b x.

  3. [M18] Prove that lg⁡3\lg 3 is irrational.

  4. [M15] Which is larger, 21002^{100} or 3633^{63}? Decide with the value lg⁡3=1.58496…\lg 3 = 1.58496\ldots, and check that the error in that value cannot change the answer.

  5. [M20] How many decimal digits does 210002^{1000} have, and what is its leading digit? Use log⁡102=0.30102 99957\log_{10} 2 = 0.30102\,99957.

  6. [M20] Let n≥1n \ge 1 have dd binary digits. Prove that n2n^2 has 2d−12d - 1 or 2d2d binary digits, and give an example of each case.

  7. [M18] Use the method of repeated squaring of the first example in the Examples part to find the first four bits after the binary point of lg⁡5\lg 5.

  8. [HM22] Prove that n1/n→1n^{1/n} \to 1 as n→∞n \to \infty. (Hint: write n1/n=1+tnn^{1/n} = 1 + t_n and use one term of the binomial theorem.)

  9. [M12] From log⁡102=0.30103\log_{10} 2 = 0.30103 and log⁡103=0.47712\log_{10} 3 = 0.47712, find log⁡105\log_{10} 5 and log⁡1072\log_{10} 72 to five places.

Lean

Mathlib has all the results of this section, but it builds them in a different order. The integer powers are Monoid.npow and zpow, with the laws pow_add, pow_mul, mul_pow, zpow_add₀, zpow_mul and mul_zpow. The real power Real.rpow is defined by Real.rpow_def_of_pos as exln⁡be^{x \ln b} for b>0b > 0; its laws are Real.rpow_add, Real.rpow_mul and Real.mul_rpow, and its agreement with the integer power is Real.rpow_natCast and Real.rpow_intCast. Bernoulli's inequality is one_add_mul_le_pow. The logarithm to the base bb is Real.logb, with Real.rpow_logb, Real.logb_mul, Real.logb_rpow, Real.logb_self_eq_one and the change of base Real.log_div_log. The number of digits is Nat.length_digits, in terms of the integer logarithm Nat.log, and Real.floor_logb_natCast relates ⌊log⁡br⌋\lfloor \log_b r \rfloor to Int.log. All of these are proved in Mathlib.

The construction of bxb^x as a least upper bound of rational powers (Definition P2.4.9, the counterpart of equations 1.2.2(7) and 1.2.2(8)) is not stated in Lean in that form. Its agreement with Real.rpow is a short exercise with Real.continuous_const_rpow and the density of the rationals.

Notes and history

Napier published the first table of logarithms in 1614, and Briggs replaced Napier's awkward scale with the base 1010 a few years later. For three centuries the common logarithm was a tool of calculation: a product became a sum of two entries in a table. The method of repeated squaring in the first example is close to the methods of the table makers. The natural logarithm and the number ee took their modern form with Euler, who also gave bxb^x its modern meaning as a function of a real variable.

In computer science the binary logarithm is the common one, because halving is the basic step of many algorithms. The short notation lg⁡\lg for log⁡2\log_2 is the one of TAOCP, and this handbook keeps it. TAOCP 1.2.2 builds the real powers from the decimal expansion of the exponent; this section uses least upper bounds of rational powers, which give the same function. GKP94, Section 9.1, puts the logarithm in its place in the scale of growth: it grows more slowly than each power nϵn^\epsilon with ϵ>0\epsilon > 0.

Floors and ceilings

Statement

The floor and the ceiling turn a real number into an integer. Both exist because the real numbers are Archimedean: for each real xx there are integers below xx and integers above xx, and a nonempty set of integers with an upper bound has a largest element. All the laws of this section follow from one pair of inequalities, the bracket ⌊x⌋≤x<⌊x⌋+1\lfloor x \rfloor \le x < \lfloor x \rfloor + 1. The art is to turn each claim about floors into a claim about inequalities between an integer and a real number, where the floor goes away.

Definition P2.4.27 (floor and ceiling). Let xx be real. The floor ⌊x⌋\lfloor x \rfloor is the largest integer that is at most xx. The ceiling ⌈x⌉\lceil x \rceil is the smallest integer that is at least xx.

Lean: Mathlib Int.floor and Int.ceil, written ⌊x⌋ and ⌈x⌉, in each FloorRing, in particular for ℝ and ℚ (proved in Mathlib). TAOCP/Notation (tamnd/taocp-lean main) uses these Mathlib functions for ⌊x⌋\lfloor x \rfloor and ⌈x⌉\lceil x \rceil.

Example P2.4.28. ⌊3.7⌋=3\lfloor 3.7 \rfloor = 3 and ⌈3.2⌉=4\lceil 3.2 \rceil = 4. For negative numbers the floor is not "drop the digits after the point": ⌊−3.7⌋=−4\lfloor -3.7 \rfloor = -4, because −4≤−3.7<−3-4 \le -3.7 < -3, and ⌈−3.2⌉=−3\lceil -3.2 \rceil = -3. For an integer both are the number itself: ⌊5⌋=⌈5⌉=5\lfloor 5 \rfloor = \lceil 5 \rceil = 5.

Example P2.4.29. Since 29=512<1000<1024=2102^9 = 512 < 1000 < 1024 = 2^{10}, lg⁡1000\lg 1000 lies between 99 and 1010, so ⌊lg⁡1000⌋=9\lfloor \lg 1000 \rfloor = 9 and ⌈lg⁡1000⌉=10\lceil \lg 1000 \rceil = 10. Also ⌊π⌋=3\lfloor \pi \rfloor = 3, ⌈2⌉=2\lceil \sqrt{2} \rceil = 2 and ⌊−2⌋=−2\lfloor -\sqrt{2} \rfloor = -2.

Theorem P2.4.30 (basic laws). Let xx and yy be real and let nn be an integer.

  1. ⌊x⌋=n\lfloor x \rfloor = n if and only if n≤x<n+1n \le x < n + 1, if and only if x−1<n≤xx - 1 < n \le x. Also ⌈x⌉=n\lceil x \rceil = n if and only if n−1<x≤nn - 1 < x \le n, if and only if x≤n<x+1x \le n < x + 1.
  2. x−1<⌊x⌋≤x≤⌈x⌉<x+1x - 1 < \lfloor x \rfloor \le x \le \lceil x \rceil < x + 1.
  3. n≤x  ⟺  n≤⌊x⌋n \le x \iff n \le \lfloor x \rfloor;   x<n  ⟺  ⌊x⌋<n\; x < n \iff \lfloor x \rfloor < n;   x≤n  ⟺  ⌈x⌉≤n\; x \le n \iff \lceil x \rceil \le n;   n<x  ⟺  n<⌈x⌉\; n < x \iff n < \lceil x \rceil.
  4. ⌊x+n⌋=⌊x⌋+n\lfloor x + n \rfloor = \lfloor x \rfloor + n and ⌈x+n⌉=⌈x⌉+n\lceil x + n \rceil = \lceil x \rceil + n.
  5. ⌊−x⌋=−⌈x⌉\lfloor -x \rfloor = -\lceil x \rceil and ⌈−x⌉=−⌊x⌋\lceil -x \rceil = -\lfloor x \rfloor.
  6. ⌈x⌉−⌊x⌋=[x∉ℤ]\lceil x \rceil - \lfloor x \rfloor = [x \notin \mathbb{Z}].
  7. If x≤yx \le y, then ⌊x⌋≤⌊y⌋\lfloor x \rfloor \le \lfloor y \rfloor and ⌈x⌉≤⌈y⌉\lceil x \rceil \le \lceil y \rceil.

Lean: Int.floor_eq_iff, Int.ceil_eq_iff, Int.floor_le, Int.lt_floor_add_one, Int.le_ceil, Int.ceil_lt_add_one, Int.le_floor, Int.floor_lt, Int.ceil_le, Int.lt_ceil, Int.floor_add_intCast, Int.ceil_add_intCast, Int.floor_neg, Int.ceil_neg, Int.floor_mono, Int.floor_le_ceil and Int.ceil_le_floor_add_one (proved in Mathlib).

Proof. Part 1 restates the definition: n=⌊x⌋n = \lfloor x \rfloor means n≤xn \le x and n+1>xn + 1 > x, since n+1n + 1 is the next integer. The second form moves 11 to the other side. The ceiling is the mirror case. Part 2 collects the inequalities of part 1. For part 3, if n≤xn \le x then nn is one of the integers at most xx, so n≤⌊x⌋n \le \lfloor x \rfloor; conversely n≤⌊x⌋≤xn \le \lfloor x \rfloor \le x. The second claim of part 3 is the negation of the first, and the other two are the mirror images. For part 4, ⌊x⌋+n\lfloor x \rfloor + n is an integer with ⌊x⌋+n≤x+n<⌊x⌋+n+1\lfloor x \rfloor + n \le x + n < \lfloor x \rfloor + n + 1, so it is ⌊x+n⌋\lfloor x + n \rfloor by part 1. For part 5, ⌈x⌉−1<x≤⌈x⌉\lceil x \rceil - 1 < x \le \lceil x \rceil gives −⌈x⌉≤−x<−⌈x⌉+1-\lceil x \rceil \le -x < -\lceil x \rceil + 1, so ⌊−x⌋=−⌈x⌉\lfloor -x \rfloor = -\lceil x \rceil; replace xx by −x-x for the second law. For part 6, if xx is an integer both sides are 00. If not, ⌊x⌋<x<⌊x⌋+1\lfloor x \rfloor < x < \lfloor x \rfloor + 1, so ⌈x⌉=⌊x⌋+1\lceil x \rceil = \lfloor x \rfloor + 1 by part 1. For part 7, ⌊x⌋≤x≤y\lfloor x \rfloor \le x \le y, so ⌊x⌋≤⌊y⌋\lfloor x \rfloor \le \lfloor y \rfloor by part 3. □\square

Theorem P2.4.31 (continuous increasing functions). Let ff be a continuous and strictly increasing function on an interval II of real numbers. Suppose that f(y)f(y) is an integer only when yy is an integer. Then for each x∈Ix \in I with ⌊x⌋∈I\lfloor x \rfloor \in I,

⌊f(x)⌋=⌊f(⌊x⌋)⌋,\lfloor f(x) \rfloor = \lfloor f(\lfloor x \rfloor) \rfloor,

and for each x∈Ix \in I with ⌈x⌉∈I\lceil x \rceil \in I, ⌈f(x)⌉=⌈f(⌈x⌉)⌉\lceil f(x) \rceil = \lceil f(\lceil x \rceil) \rceil.

Lean: not yet stated in Lean in this general form; special cases are Int.floor_div_natCast and Real.floor_real_sqrt_eq_nat_sqrt (proved in Mathlib).

Proof. If xx is an integer there is nothing to prove. Otherwise ⌊x⌋<x\lfloor x \rfloor < x, so f(⌊x⌋)<f(x)f(\lfloor x \rfloor) < f(x), and part 7 of the basic laws gives ⌊f(⌊x⌋)⌋≤⌊f(x)⌋\lfloor f(\lfloor x \rfloor) \rfloor \le \lfloor f(x) \rfloor. Suppose the inequality is strict, and let m=⌊f(x)⌋m = \lfloor f(x) \rfloor. Then m≤f(x)m \le f(x), and m>⌊f(⌊x⌋)⌋m > \lfloor f(\lfloor x \rfloor) \rfloor gives m>f(⌊x⌋)m > f(\lfloor x \rfloor) by part 3. By the intermediate value theorem there is yy with ⌊x⌋<y≤x\lfloor x \rfloor < y \le x and f(y)=mf(y) = m. By the assumption, yy is an integer. But there is no integer in the interval (⌊x⌋,x](\lfloor x \rfloor, x], since the next integer after ⌊x⌋\lfloor x \rfloor is larger than xx. This contradiction proves the law for floors. The law for ceilings has the mirror proof. □\square

Example P2.4.32. The condition on integer values cannot be left out. Let f(x)=2xf(x) = 2x and x=12x = \frac{1}{2}. Then ⌊f(x)⌋=1\lfloor f(x) \rfloor = 1 but ⌊f(⌊x⌋)⌋=⌊0⌋=0\lfloor f(\lfloor x \rfloor) \rfloor = \lfloor 0 \rfloor = 0. The function takes the integer value 11 at the point 12\frac{1}{2}, which is not an integer.

Corollary P2.4.33 (nested floors). Let xx be real, let mm be an integer and let nn be a positive integer. Then

⌊x+mn⌋=⌊⌊x⌋+mn⌋,⌈x+mn⌉=⌈⌈x⌉+mn⌉.\Bigl\lfloor \frac{x + m}{n} \Bigr\rfloor = \Bigl\lfloor \frac{\lfloor x \rfloor + m}{n} \Bigr\rfloor, \qquad \Bigl\lceil \frac{x + m}{n} \Bigr\rceil = \Bigl\lceil \frac{\lceil x \rceil + m}{n} \Bigr\rceil.

In particular ⌊⌊y⌋/n⌋=⌊y/n⌋\lfloor \lfloor y \rfloor / n \rfloor = \lfloor y / n \rfloor for each real yy, and so ⌊⌊x/a⌋/b⌋=⌊x/(ab)⌋\lfloor \lfloor x/a \rfloor / b \rfloor = \lfloor x/(ab) \rfloor for each real a>0a > 0 and each positive integer bb.

Lean: Int.floor_div_natCast states ⌊y/n⌋=⌊y⌋/n\lfloor y/n \rfloor = \lfloor y \rfloor / n, where the division on the right is the integer division Int.ediv (proved in Mathlib).

Proof. The function f(t)=(t+m)/nf(t) = (t + m)/n is continuous and strictly increasing on ℝ\mathbb{R}. If f(t)=kf(t) = k is an integer, then t=kn−mt = kn - m is an integer. The theorem applies on I=ℝI = \mathbb{R}. For the last claim take y=x/ay = x/a and n=bn = b. □\square

Theorem P2.4.34 (integer quotients). Let nn be an integer and mm a positive integer. Then q=⌊n/m⌋q = \lfloor n/m \rfloor is the unique integer with n=qm+rn = qm + r and 0≤r<m0 \le r < m. Also

⌈nm⌉=⌊n+m−1m⌋=⌊n−1m⌋+1.\Bigl\lceil \frac{n}{m} \Bigr\rceil = \Bigl\lfloor \frac{n + m - 1}{m} \Bigr\rfloor = \Bigl\lfloor \frac{n - 1}{m} \Bigr\rfloor + 1.

Lean: Rat.floor_intCast_div_natCast states ⌊n/m⌋=n/m\lfloor n/m \rfloor = n / m with the integer division on the right; Int.sub_floor_div_mul_nonneg and Int.sub_floor_div_mul_lt give 0≤r<m0 \le r < m (proved in Mathlib).

Proof. Let q=⌊n/m⌋q = \lfloor n/m \rfloor and r=n−qmr = n - qm. The bracket q≤n/m<q+1q \le n/m < q + 1, multiplied by m>0m > 0, gives qm≤n<qm+mqm \le n < qm + m, that is 0≤r<m0 \le r < m. If also n=q′m+r′n = q'm + r' with 0≤r′<m0 \le r' < m, then q′≤n/m<q′+1q' \le n/m < q' + 1, so q′=⌊n/m⌋=qq' = \lfloor n/m \rfloor = q by part 1 of the basic laws. For the ceiling, let kk be an integer. Then ⌈n/m⌉≤k\lceil n/m \rceil \le k holds if and only if n≤kmn \le km, by part 3. For integers this is the same as n<km+1n < km + 1, which is the same as n+m−1<(k+1)mn + m - 1 < (k+1)m, that is (n+m−1)/m<k+1(n + m - 1)/m < k + 1. By part 3 this holds if and only if ⌊(n+m−1)/m⌋≤k\lfloor (n + m - 1)/m \rfloor \le k. Two integers with the same set of upper bounds among the integers are equal. The last form is part 4 of the basic laws: ⌊(n+m−1)/m⌋=⌊(n−1)/m+1⌋\lfloor (n + m - 1)/m \rfloor = \lfloor (n - 1)/m + 1 \rfloor. □\square

Theorem P2.4.35 (floors of roots). Let k≥1k \ge 1 be an integer.

  1. For each integer n≥0n \ge 0, ⌊n1/k⌋\lfloor n^{1/k} \rfloor is the unique integer a≥0a \ge 0 with ak≤n<(a+1)ka^k \le n < (a+1)^k.
  2. For each real x≥0x \ge 0, ⌊⌊x⌋1/k⌋=⌊x1/k⌋\lfloor \lfloor x \rfloor^{1/k} \rfloor = \lfloor x^{1/k} \rfloor and ⌈⌈x⌉1/k⌉=⌈x1/k⌉\lceil \lceil x \rceil^{1/k} \rceil = \lceil x^{1/k} \rceil.

Lean: for k=2k = 2, Real.floor_real_sqrt_eq_nat_sqrt states ⌊n⌋=\lfloor \sqrt{n} \rfloor = Nat.sqrt n, and Nat.sqrt_le' and Nat.lt_succ_sqrt' give a2≤n<(a+1)2a^2 \le n < (a+1)^2 for a=a = Nat.sqrt n (proved in Mathlib).

Proof. For part 1, let a=⌊n1/k⌋a = \lfloor n^{1/k} \rfloor. Then a≤n1/k<a+1a \le n^{1/k} < a + 1, and the kkth power is increasing on the nonnegative reals, so ak≤n<(a+1)ka^k \le n < (a+1)^k. Conversely, an integer a≥0a \ge 0 with ak≤n<(a+1)ka^k \le n < (a+1)^k satisfies a≤n1/k<a+1a \le n^{1/k} < a + 1, so it is the floor. For part 2, apply the theorem on continuous increasing functions to f(t)=t1/kf(t) = t^{1/k} on I=[0,∞)I = [0, \infty). If f(t)=jf(t) = j is an integer, then t=jkt = j^k is an integer. □\square

Theorem P2.4.36 (counting integers in an interval). Let α≤β\alpha \le \beta be real. The number of integers in the interval

[α,β] is ⌊β⌋−⌈α⌉+1,[α,β) is ⌈β⌉−⌈α⌉,(α,β] is ⌊β⌋−⌊α⌋,[\alpha, \beta] \text{ is } \lfloor \beta \rfloor - \lceil \alpha \rceil + 1, \qquad [\alpha, \beta) \text{ is } \lceil \beta \rceil - \lceil \alpha \rceil, \qquad (\alpha, \beta] \text{ is } \lfloor \beta \rfloor - \lfloor \alpha \rfloor,

and, when α<β\alpha < \beta, the number in (α,β)(\alpha, \beta) is ⌈β⌉−⌊α⌋−1\lceil \beta \rceil - \lfloor \alpha \rfloor - 1. For integers n≥0n \ge 0 and m≥1m \ge 1, the number of multiples of mm among 1,2,…,n1, 2, \ldots, n is ⌊n/m⌋\lfloor n/m \rfloor.

Lean: Int.card_Icc, Int.card_Ico, Int.card_Icc_of_le and Int.card_Ico_of_le for integer end points, and Nat.Ioc_filter_dvd_card_eq_div for the multiples (proved in Mathlib).

Proof. By part 3 of the basic laws, an integer kk satisfies α≤k\alpha \le k if and only if ⌈α⌉≤k\lceil \alpha \rceil \le k, and k≤βk \le \beta if and only if k≤⌊β⌋k \le \lfloor \beta \rfloor. So the integers in [α,β][\alpha, \beta] are the integers from ⌈α⌉\lceil \alpha \rceil to ⌊β⌋\lfloor \beta \rfloor. There are ⌊β⌋−⌈α⌉+1\lfloor \beta \rfloor - \lceil \alpha \rceil + 1 of them. This number is not negative, since ⌈α⌉<α+1≤β+1<⌊β⌋+2\lceil \alpha \rceil < \alpha + 1 \le \beta + 1 < \lfloor \beta \rfloor + 2. In the same way, k<βk < \beta if and only if k≤⌈β⌉−1k \le \lceil \beta \rceil - 1, and α<k\alpha < k if and only if ⌊α⌋+1≤k\lfloor \alpha \rfloor + 1 \le k. These give the other three counts; the condition α<β\alpha < \beta for the open interval excludes the case α=β∈ℤ\alpha = \beta \in \mathbb{Z}, where the formula gives −1-1. The multiples of mm in [1,n][1, n] are jmjm with 1/m≤j≤n/m1/m \le j \le n/m, and there are ⌊n/m⌋−⌈1/m⌉+1=⌊n/m⌋\lfloor n/m \rfloor - \lceil 1/m \rceil + 1 = \lfloor n/m \rfloor of them. □\square

Example P2.4.37. The interval [2,π][\sqrt{2}, \pi] contains ⌊π⌋−⌈2⌉+1=3−2+1=2\lfloor \pi \rfloor - \lceil \sqrt{2} \rceil + 1 = 3 - 2 + 1 = 2 integers, namely 22 and 33. The numbers 1,…,10001, \ldots, 1000 contain ⌊1000/7⌋=142\lfloor 1000/7 \rfloor = 142 multiples of 77. By inclusion and exclusion, they contain ⌊1000/3⌋+⌊1000/5⌋−⌊1000/15⌋=333+200−66=467\lfloor 1000/3 \rfloor + \lfloor 1000/5 \rfloor - \lfloor 1000/15 \rfloor = 333 + 200 - 66 = 467 numbers that are divisible by 33 or by 55.

Proof

Each proof of this section has the same plan. A statement about ⌊x⌋\lfloor x \rfloor is replaced by a statement about an integer nn and the bracket n≤x<n+1n \le x < n + 1, and the floor sign disappears. Part 3 of the basic laws is the tool for this. It says that "n≤⌊x⌋n \le \lfloor x \rfloor" and "n≤xn \le x" are the same statement when nn is an integer. Two integers are equal when they have the same integer upper bounds, so a claim ⌊u⌋=⌊v⌋\lfloor u \rfloor = \lfloor v \rfloor follows from "n≤u  ⟺  n≤vn \le u \iff n \le v for each integer nn". The proof of the formula for ⌈n/m⌉\lceil n/m \rceil is an example of this plan.

The theorem on continuous increasing functions is the one result with a different flavor. It uses the intermediate value theorem, and it explains when an inner floor can be removed: exactly when the outer function cannot reach a new integer between ⌊x⌋\lfloor x \rfloor and xx.

Examples

Example P2.4.38. The integer square root by Newton's method. Let n≥1n \ge 1 and s=⌊n⌋s = \lfloor \sqrt{n} \rfloor. Start with any integer x0≥sx_0 \ge s, for example x0=nx_0 = n, and put xj+1=⌊(xj+⌊n/xj⌋)/2⌋x_{j+1} = \lfloor (x_j + \lfloor n/x_j \rfloor)/2 \rfloor. Stop at the first jj with xj+1≥xjx_{j+1} \ge x_j. Then xj=sx_j = s. For the proof, first note that xj+1=⌊(xj+n/xj)/2⌋x_{j+1} = \lfloor (x_j + n/x_j)/2 \rfloor by the corollary on nested floors, applied to the real number n/xjn/x_j, the integer xjx_j and the divisor 22. By the inequality of the arithmetic and geometric means, (xj+n/xj)/2≥n(x_j + n/x_j)/2 \ge \sqrt{n}, so xj+1≥sx_{j+1} \ge s at every step. If xj>sx_j > s, then xj≥s+1>nx_j \ge s + 1 > \sqrt{n}, so n/xj<xjn/x_j < x_j and ⌊n/xj⌋≤xj−1\lfloor n/x_j \rfloor \le x_j - 1, which gives xj+1≤⌊(2xj−1)/2⌋=xj−1x_{j+1} \le \lfloor (2x_j - 1)/2 \rfloor = x_j - 1. So the sequence decreases while it is above ss, and it cannot go below ss. For n=1000n = 1000 the values are 1000,500,251,127,67,40,32,311000, 500, 251, 127, 67, 40, 32, 31, and the next value is ⌊(31+32)/2⌋=31\lfloor (31 + 32)/2 \rfloor = 31, so ⌊1000⌋=31\lfloor \sqrt{1000} \rfloor = 31. Check: 312=961≤1000<1024=32231^2 = 961 \le 1000 < 1024 = 32^2.

Example P2.4.39. Binary search in a table of nn items looks at the middle position ⌊(l+u)/2⌋\lfloor (l + u)/2 \rfloor of the current range l…ul \ldots u. A range of N=u−l+1N = u - l + 1 items splits into parts of ⌊(N−1)/2⌋\lfloor (N-1)/2 \rfloor and ⌈(N−1)/2⌉\lceil (N-1)/2 \rceil items around the middle. By the corollary on nested floors, ⌊⌊N/2⌋/2⌋=⌊N/4⌋\lfloor \lfloor N/2 \rfloor / 2 \rfloor = \lfloor N/4 \rfloor, and after jj halvings at most ⌊N/2j⌋\lfloor N / 2^j \rfloor items are left. So the search ends after at most ⌊lg⁡N⌋+1\lfloor \lg N \rfloor + 1 comparisons, the number of binary digits of NN (Section 4.1).

Example P2.4.40. The year YY of the Gregorian calendar is a leap year when 4∣Y4 \mid Y, except when 100∣Y100 \mid Y and 400∤Y400 \nmid Y. By the theorem on counting, the number of leap years among the years 1,…,Y1, \ldots, Y is ⌊Y/4⌋−⌊Y/100⌋+⌊Y/400⌋\lfloor Y/4 \rfloor - \lfloor Y/100 \rfloor + \lfloor Y/400 \rfloor. For Y=2000Y = 2000 this is 500−20+5=485500 - 20 + 5 = 485. Such counts occur in each formula for the day of the week.

Example P2.4.41. The integers in (α,β](\alpha, \beta] number ⌊β⌋−⌊α⌋\lfloor \beta \rfloor - \lfloor \alpha \rfloor, and this count is additive: for α≤β≤γ\alpha \le \beta \le \gamma the counts for (α,β](\alpha, \beta] and (β,γ](\beta, \gamma] add up to the count for (α,γ](\alpha, \gamma]. Half open intervals of this kind tile the line, and this is the reason that many formulas with floors are cleanest with intervals that are open on one side.

Exercises

  1. [05] Find ⌊−0.5⌋\lfloor -0.5 \rfloor, ⌈−0.5⌉\lceil -0.5 \rceil and ⌊⌈2.3⌉−0.5⌋\lfloor \lceil 2.3 \rceil - 0.5 \rfloor.

  2. [10] Prove that ⌊x⌋+⌊−x⌋=−[x∉ℤ]\lfloor x \rfloor + \lfloor -x \rfloor = -[x \notin \mathbb{Z}] for each real xx.

  3. [M12] Prove that ⌊x+y⌋\lfloor x + y \rfloor is ⌊x⌋+⌊y⌋\lfloor x \rfloor + \lfloor y \rfloor or ⌊x⌋+⌊y⌋+1\lfloor x \rfloor + \lfloor y \rfloor + 1, and that ⌈x+y⌉\lceil x + y \rceil is ⌈x⌉+⌈y⌉−1\lceil x \rceil + \lceil y \rceil - 1 or ⌈x⌉+⌈y⌉\lceil x \rceil + \lceil y \rceil. Give examples of all four cases.

  4. [M15] Prove that ⌊2x⌋=2⌊x⌋\lfloor 2x \rfloor = 2 \lfloor x \rfloor if x−⌊x⌋<12x - \lfloor x \rfloor < \frac{1}{2}, and ⌊2x⌋=2⌊x⌋+1\lfloor 2x \rfloor = 2 \lfloor x \rfloor + 1 otherwise.

  5. [10] A disk stores data in blocks of mm bytes, numbered from 00. Prove that a file of n≥1n \ge 1 bytes needs ⌈n/m⌉\lceil n/m \rceil blocks, and that byte number jj of the file, counted from 00, lies in block ⌊j/m⌋\lfloor j/m \rfloor.

  6. [M15] Prove or disprove, for all real x≥0x \ge 0: (a) ⌊⌈x⌉⌋=⌊x⌋\lfloor \sqrt{\lceil x \rceil} \rfloor = \lfloor \sqrt{x} \rfloor; (b) ⌈⌊x⌋⌉=⌈x⌉\lceil \sqrt{\lfloor x \rfloor} \rceil = \lceil \sqrt{x} \rceil.

  7. [M15] How many of the integers 1,2,…,10001, 2, \ldots, 1000 are perfect squares or perfect cubes?

  8. [M20] Let α>0\alpha > 0 be real and N≥0N \ge 0 an integer. Prove that the number of integers k≥1k \ge 1 with ⌊kα⌋≤N\lfloor k\alpha \rfloor \le N is ⌈(N+1)/α⌉−1\lceil (N+1)/\alpha \rceil - 1.

  9. [M15] Prove that ⌊⌊x⌋⌋=⌊x1/4⌋\lfloor \sqrt{\lfloor \sqrt{x} \rfloor} \rfloor = \lfloor x^{1/4} \rfloor for each real x≥0x \ge 0.

  10. [M22] Find all real xx with ⌊x2⌋=⌊x⌋2\lfloor x^2 \rfloor = \lfloor x \rfloor^2.

Lean

Mathlib defines the floor and the ceiling in each ordered ring with a FloorRing instance, and the real and the rational numbers have such instances. The basic laws are Int.floor_eq_iff, Int.ceil_eq_iff, Int.floor_le, Int.lt_floor_add_one, Int.le_floor, Int.floor_lt, Int.le_ceil, Int.ceil_le, Int.lt_ceil, Int.floor_add_intCast, Int.ceil_add_intCast, Int.floor_neg, Int.ceil_neg and Int.floor_mono. The bounds for a sum are Int.le_floor_add and Int.le_floor_add_floor. The floor of a quotient is Int.floor_div_natCast and Rat.floor_intCast_div_natCast, the floor of a square root is Real.floor_real_sqrt_eq_nat_sqrt, and the counts of integers in an interval are Int.card_Icc, Int.card_Ico and Nat.Ioc_filter_dvd_card_eq_div. All of these are proved in Mathlib. TAOCP/Notation (tamnd/taocp-lean main) uses Int.floor and Int.ceil for ⌊x⌋\lfloor x \rfloor and ⌈x⌉\lceil x \rceil and adds no new definition.

The theorem on continuous increasing functions is not yet stated in Lean in its general form.

Notes and history

The words floor and ceiling and the signs ⌊x⌋\lfloor x \rfloor and ⌈x⌉\lceil x \rceil were introduced by Kenneth Iverson in 1962, in his language APL. Before that the floor was written [x][x], after Gauss, and the bracket is still common in number theory. The sign [x][x] has the defect that it gives no ceiling, and many formulas need both functions.

TAOCP 1.2.4 gives the definitions and the first laws of floors and ceilings and leaves most of the proofs to its exercises. GKP94, Sections 3.1 and 3.2, prove the same laws and the rule for continuous increasing functions, and they use the rule to count the integers in intervals. The integer square root of the first example is the Newton iteration for x2=nx^2 = n carried out with integer arithmetic only.

The mod operation

Statement

Division of an integer xx by a positive integer yy gives a quotient and a remainder. This section defines the remainder x mod yx \bmod y for all real numbers xx and yy, with no condition on the signs and with a value also for y=0y = 0. The definition has one formula, and the floor of Section 4.2 does all the work. The general form pays off: angles, clock times and memory addresses all reduce by  mod \bmod, and the distributive law c(x mod y)=(cx) mod (cy)c(x \bmod y) = (cx) \bmod (cy) moves freely between integers and reals.

Definition P2.4.42 (mod). Let xx and yy be real. If y≠0y \ne 0, then

x mod y=x−y⌊xy⌋.x \bmod y = x - y \Bigl\lfloor \frac{x}{y} \Bigr\rfloor.

If y=0y = 0, then x mod 0=xx \bmod 0 = x. The number ⌊x/y⌋\lfloor x/y \rfloor is the quotient and x mod yx \bmod y is the remainder. The operation  mod \bmod binds like multiplication: a+b mod ca + b \bmod c means a+(b mod c)a + (b \bmod c).

Lean: TAOCP.kmod x y, defined as x - y * ⌊x / y⌋ over a field with a FloorRing, in TAOCP/Notation (tamnd/taocp-lean main); TAOCP.kmod_def unfolds it, and TAOCP.kmod_zero states x mod 0=xx \bmod 0 = x. Since Lean defines x/0=0x/0 = 0, the one formula already gives the value for y=0y = 0.

Example P2.4.43. For integers: 7 mod 3=7−3⋅2=17 \bmod 3 = 7 - 3 \cdot 2 = 1, (−7) mod 3=−7−3⋅(−3)=2(-7) \bmod 3 = -7 - 3 \cdot (-3) = 2, 7 mod (−3)=7−(−3)(−3)=−27 \bmod (-3) = 7 - (-3)(-3) = -2 and (−7) mod (−3)=−7−(−3)⋅2=−1(-7) \bmod (-3) = -7 - (-3) \cdot 2 = -1. The remainder has the sign of the divisor yy, not the sign of xx.

Example P2.4.44. For reals: 5.5 mod 2=5.5−2⋅2=1.55.5 \bmod 2 = 5.5 - 2 \cdot 2 = 1.5, 2.25 mod 0.5=2.25−0.5⋅4=0.252.25 \bmod 0.5 = 2.25 - 0.5 \cdot 4 = 0.25 and (−2.25) mod 0.5=−2.25−0.5⋅(−5)=0.25(-2.25) \bmod 0.5 = -2.25 - 0.5 \cdot (-5) = 0.25. Also π mod 1=0.14159…\pi \bmod 1 = 0.14159\ldots and x mod 0=xx \bmod 0 = x for each xx, for example 13 mod 0=1313 \bmod 0 = 13.

Theorem P2.4.45 (range of the remainder). Let xx and y≠0y \ne 0 be real. Then x−x mod y=y⌊x/y⌋x - x \bmod y = y \lfloor x/y \rfloor is an integer multiple of yy, and

0≤x mod y<yif y>0,y<x mod y≤0if y<0.0 \le x \bmod y < y \quad \text{if } y > 0, \qquad y < x \bmod y \le 0 \quad \text{if } y < 0.

In particular x mod y=xx \bmod y = x when 0≤x<y0 \le x < y, and ∣x mod y∣<∣y∣|x \bmod y| < |y|.

Lean: TAOCP.floor_mul_add_kmod states y⌊x/y⌋+x mod y=xy \lfloor x/y \rfloor + x \bmod y = x; for y>0y > 0 the range is Int.sub_floor_div_mul_nonneg and Int.sub_floor_div_mul_lt (proved in Mathlib). The range for y<0y < 0 is not yet stated for TAOCP.kmod.

Proof. The first claim is the definition. Let q=⌊x/y⌋q = \lfloor x/y \rfloor, so 0≤x/y−q<10 \le x/y - q < 1. If y>0y > 0, multiply by yy: 0≤x−qy<y0 \le x - qy < y. If y<0y < 0, multiplication by yy reverses the inequalities: y<x−qy≤0y < x - qy \le 0. If 0≤x<y0 \le x < y, then ⌊x/y⌋=0\lfloor x/y \rfloor = 0 and x mod y=xx \bmod y = x. □\square

Theorem P2.4.46 (characterization of the remainder). Let y≠0y \ne 0, and let JyJ_y be the interval [0,y)[0, y) if y>0y > 0 and (y,0](y, 0] if y<0y < 0. For each real xx the remainder x mod yx \bmod y is the unique r∈Jyr \in J_y such that x−rx - r is an integer multiple of yy. As a consequence, for real xx, x′x' and each integer kk:

  1. (x+ky) mod y=x mod y(x + ky) \bmod y = x \bmod y;
  2. (x mod y) mod y=x mod y(x \bmod y) \bmod y = x \bmod y;
  3. x mod y=x′ mod yx \bmod y = x' \bmod y if and only if x−x′x - x' is an integer multiple of yy;
  4. x mod y=0x \bmod y = 0 if and only if x/yx/y is an integer.

Lean: for integers, Int.emod_emod and Int.add_mul_emod_self_right (proved in Lean core); for TAOCP.kmod these consequences are not yet stated.

Proof. By the theorem on the range, r=x mod yr = x \bmod y has the two properties. Let r,r′∈Jyr, r' \in J_y with x−r=kyx - r = ky and x−r′=k′yx - r' = k'y for integers kk and k′k'. Then r′−r=(k−k′)yr' - r = (k - k')y. Both rr and r′r' lie in an interval of length ∣y∣|y| that is open at one end, so ∣r′−r∣<∣y∣|r' - r| < |y|, and ∣k−k′∣<1|k - k'| < 1. So k=k′k = k' and r=r′r = r'. For part 1, x mod yx \bmod y is in JyJ_y and (x+ky)−x mod y(x + ky) - x \bmod y is a multiple of yy, so it is the remainder of x+kyx + ky. Part 2 is the case where xx is replaced by x mod yx \bmod y, which differs from xx by a multiple of yy. For part 3, if x−x′=kyx - x' = ky, part 1 gives the equality. Conversely, if the remainders are equal, then x−x′=(x−x mod y)−(x′−x′ mod y)x - x' = (x - x \bmod y) - (x' - x' \bmod y) is a difference of two multiples of yy. Part 4 is part 3 with x′=0x' = 0. □\square

Definition P2.4.47 (fractional part). The fractional part of a real number xx is x mod 1=x−⌊x⌋x \bmod 1 = x - \lfloor x \rfloor. It is often written {x}\{x\}, when no confusion with a set is possible. So x=⌊x⌋+x mod 1x = \lfloor x \rfloor + x \bmod 1 and 0≤x mod 1<10 \le x \bmod 1 < 1.

Lean: Mathlib Int.fract x, with Int.self_sub_floor (x−⌊x⌋=x - \lfloor x \rfloor = Int.fract x), Int.floor_add_fract, Int.fract_nonneg, Int.fract_lt_one and Int.fract_add_intCast (proved in Mathlib).

Example P2.4.48. 3.25 mod 1=0.253.25 \bmod 1 = 0.25, while (−3.25) mod 1=−3.25−(−4)=0.75(-3.25) \bmod 1 = -3.25 - (-4) = 0.75. The fractional part of a negative number is not the digits after the point. For each xx that is not an integer, x mod 1+(−x) mod 1=1x \bmod 1 + (-x) \bmod 1 = 1.

Example P2.4.49. The fractional parts of n2n\sqrt{2} for n=1,2,3,4n = 1, 2, 3, 4 are 0.414210.41421, 0.828430.82843, 0.242640.24264 and 0.656850.65685, to five places. Since 2\sqrt{2} is irrational, no two of the numbers n2 mod 1n\sqrt{2} \bmod 1 are equal: from n2−m2∈ℤn\sqrt{2} - m\sqrt{2} \in \mathbb{Z} with n≠mn \ne m it would follow that 2\sqrt{2} is rational. Such sequences return in the theory of uniform distribution and in hashing by multiplication.

Theorem P2.4.50 (distributive law). For all real cc, xx and yy,

c (x mod y)=(cx) mod (cy).c\,(x \bmod y) = (cx) \bmod (cy).

Lean: for integers and c>0c > 0, Int.mul_fmod_mul_of_pos (proved in Lean core); for TAOCP.kmod the law is not yet stated.

Proof. If y=0y = 0, both sides are cxcx, since x mod 0=xx \bmod 0 = x and (cx) mod 0=cx(cx) \bmod 0 = cx. If c=0c = 0, both sides are 00: the left side is 00, and the right side is 0 mod 0=00 \bmod 0 = 0. If c≠0c \ne 0 and y≠0y \ne 0, then cx/(cy)=x/ycx/(cy) = x/y, so (cx) mod (cy)=cx−cy⌊x/y⌋=c(x−y⌊x/y⌋)(cx) \bmod (cy) = cx - cy \lfloor x/y \rfloor = c(x - y\lfloor x/y \rfloor). □\square

Example P2.4.51. The law turns a remainder of decimals into a remainder of integers. Take c=10c = 10: 10(3.5 mod 1.5)=35 mod 15=510(3.5 \bmod 1.5) = 35 \bmod 15 = 5, so 3.5 mod 1.5=0.53.5 \bmod 1.5 = 0.5. With c=−1c = -1 the law gives x mod (−y)=−((−x) mod y)x \bmod (-y) = -\bigl((-x) \bmod y\bigr), so the remainders for a negative divisor follow from the remainders for a positive one: 7 mod (−3)=−((−7) mod 3)=−27 \bmod (-3) = -\bigl((-7) \bmod 3\bigr) = -2.

Theorem P2.4.52 (integer remainders). Let aa and bb be integers. Then a mod ba \bmod b is an integer. If b>0b > 0, then ⌊a/b⌋\lfloor a/b \rfloor and a mod ba \bmod b are the quotient and the remainder of the division of aa by bb with 0≤a mod b<b0 \le a \bmod b < b. For all bb, including b≤0b \le 0, a mod ba \bmod b equals the remainder of Lean's floor division Int.fmod a b.

Lean: TAOCP.kmod_intCast_natCast states that kmod a d is a % d (the remainder Int.emod) for a natural number dd; Int.fmod_def states a.fmod b = a - b * a.fdiv b, Int.fmod_zero gives the value for b=0b = 0, and Int.fmod_eq_emod_of_nonneg and Int.emod_def relate the two remainders for b≥0b \ge 0 (proved in Lean core and in TAOCP/Notation). A bridge from TAOCP.kmod to Int.fmod for negative divisors is not yet stated.

Proof. The quotient ⌊a/b⌋\lfloor a/b \rfloor is an integer, so a−b⌊a/b⌋a - b\lfloor a/b \rfloor is an integer. For b>0b > 0 this is the theorem on integer quotients of Section 4.2. Lean's Int.fdiv a b is defined as the floor of a/ba/b for b≠0b \ne 0 and as 00 for b=0b = 0, and Int.fmod a b is a−b⋅a - b \cdot Int.fdiv a b, which is the definition of this section in both cases. □\square

Example P2.4.53. Programming languages differ for negative operands. For the pairs (a,b)=(7,3),(−7,3),(7,−3),(−7,−3)(a, b) = (7, 3), (-7, 3), (7, -3), (-7, -3), the operation of this section gives the remainders 1,2,−2,−11, 2, -2, -1. Lean's Int.emod (the operator % on Int, also used by Mathlib) gives 1,2,1,21, 2, 1, 2: its remainder is never negative. The truncating remainder Int.tmod, which is the % of C and Java, gives 1,−1,1,−11, -1, 1, -1: its remainder has the sign of aa. All three agree when a≥0a \ge 0 and b>0b > 0.

Proposition P2.4.54 (sums and nested remainders). Let xx, x′x' and zz be real.

  1. (x+x′) mod z=(x mod z+x′ mod z) mod z(x + x') \bmod z = \bigl(x \bmod z + x' \bmod z\bigr) \bmod z.
  2. For each nonzero integer mm, (x mod mz) mod z=x mod z(x \bmod mz) \bmod z = x \bmod z.

Lean: for integers, Int.add_emod, Int.emod_emod_of_dvd and Int.fmod_fmod_of_dvd (proved in Lean core).

Proof. If z=0z = 0, both parts say x+x′=x+x′x + x' = x + x' and x=xx = x. Let z≠0z \ne 0. For part 1, x−x mod zx - x \bmod z and x′−x′ mod zx' - x' \bmod z are integer multiples of zz, so the two numbers x+x′x + x' and x mod z+x′ mod zx \bmod z + x' \bmod z differ by a multiple of zz. Part 3 of the characterization gives equal remainders. For part 2, x−x mod mzx - x \bmod mz is an integer multiple of mzmz, so it is an integer multiple of zz; part 3 of the characterization again gives the claim. □\square

Proof

The definition of x mod yx \bmod y is one line, and each law of this section follows from the bracket 0≤x/y−⌊x/y⌋<10 \le x/y - \lfloor x/y \rfloor < 1 of Section 4.2. The theorem on the range multiplies the bracket by yy; the characterization adds uniqueness; the distributive law notes that the quotient x/yx/y does not change when both numbers are multiplied by cc. The value x mod 0=xx \bmod 0 = x is the one that keeps the distributive law true for c=0c = 0 and for y=0y = 0, and it agrees with Lean's convention x/0=0x/0 = 0.

The characterization is the form to use in proofs. It says that a remainder is determined by two facts: the difference x−rx - r is a multiple of yy, and rr lies in the half open interval JyJ_y. Each law about sums, products or nested remainders checks the first fact and reads off the second.

Examples

Example P2.4.55. A clock shows the hour h mod 12h \bmod 12, and tt hours after the hour hh it shows (h+t) mod 12(h + t) \bmod 12; with 12 mod 12=012 \bmod 12 = 0, the clock face shows 00 in the place of 1212. The days of the week work in the same way: since 100 mod 7=2100 \bmod 7 = 2, the day 100100 days after a Monday is a Wednesday. Part 1 of the proposition on sums says that the clock can add the hours in any order and reduce at any time.

Example P2.4.56. An angle θ\theta in radians reduces to the interval [0,2π)[0, 2\pi) as θ mod 2π\theta \bmod 2\pi. For θ=10\theta = 10, ⌊10/2π⌋=1\lfloor 10/2\pi \rfloor = 1 and 10 mod 2π=10−2π=3.7168110 \bmod 2\pi = 10 - 2\pi = 3.71681. A program that computes sin⁡θ\sin \theta for large θ\theta makes this reduction first, and the rounding error of 2π2\pi multiplied by the quotient sets a limit to its accuracy.

Example P2.4.57. A memory address aa must often be a multiple of 88. The smallest multiple of 88 that is at least aa is 8⌈a/8⌉8\lceil a/8 \rceil, and the gap to it is (−a) mod 8(-a) \bmod 8, by Exercise 9. For a=1234567a = 1234567, a mod 8=7a \bmod 8 = 7, so (−a) mod 8=1(-a) \bmod 8 = 1, and the aligned address is 12345681234568.

Example P2.4.58. The algorithm of Euclid replaces the pair (m,n)(m, n) by (n,m mod n)(n, m \bmod n) until the second number is 00. For (544,119)(544, 119) the remainders are 544 mod 119=68544 \bmod 119 = 68, 119 mod 68=51119 \bmod 68 = 51, 68 mod 51=1768 \bmod 51 = 17 and 51 mod 17=051 \bmod 17 = 0, so the last nonzero number is 1717. Chapter 5 proves that it is the greatest common divisor, and Exercise 7 shows that the remainders shrink fast.

Exercises

  1. [05] Find 13 mod 513 \bmod 5, (−13) mod 5(-13) \bmod 5, 13 mod (−5)13 \bmod (-5), (−13) mod (−5)(-13) \bmod (-5) and 13 mod 013 \bmod 0.

  2. [08] Find 7.5 mod 27.5 \bmod 2, (−7.5) mod 2(-7.5) \bmod 2 and 7.5 mod (−2)7.5 \bmod (-2).

  3. [M12] Let y≠0y \ne 0. Prove that x mod y+(−x) mod yx \bmod y + (-x) \bmod y is 00 when x/yx/y is an integer and yy otherwise.

  4. [08] Prove that x mod y=y (x/y mod 1)x \bmod y = y\,(x/y \bmod 1) for all real xx and y≠0y \ne 0.

  5. [M15] Prove that (ab) mod n=((a mod n)(b mod n)) mod n(ab) \bmod n = \bigl((a \bmod n)(b \bmod n)\bigr) \bmod n for all integers aa, bb and nn. Show by an example that the rule fails for some real aa and bb with n=1n = 1.

  6. [10] Use the distributive law to find 3.7 mod 0.43.7 \bmod 0.4.

  7. [M15] Let 0<y≤x0 < y \le x. Prove that x mod y<x/2x \bmod y < x/2.

  8. [M12] The sawtooth function is ((x))=x mod 1−12((x)) = x \bmod 1 - \frac{1}{2} when xx is not an integer, and ((x))=0((x)) = 0 when xx is an integer. Prove that ((−x))=−((x))((-x)) = -((x)) and ((x+1))=((x))((x + 1)) = ((x)) for each real xx.

  9. [M10] Let y>0y > 0. Prove that y⌈x/y⌉−x=(−x) mod yy \lceil x/y \rceil - x = (-x) \bmod y for each real xx.

  10. [M10] Prove that x mod (−y)=−((−x) mod y)x \bmod (-y) = -\bigl((-x) \bmod y\bigr) for all real xx and yy.

Lean

TAOCP/Notation in tamnd/taocp-lean main defines the operation of this section as TAOCP.kmod x y := x - y * ⌊x / y⌋ over a field with a linear order and a FloorRing. It proves TAOCP.kmod_def, TAOCP.kmod_zero (x mod 0=xx \bmod 0 = x), TAOCP.floor_mul_add_kmod (y⌊x/y⌋+x mod y=xy \lfloor x/y \rfloor + x \bmod y = x) and the bridge TAOCP.kmod_intCast_natCast to the integer remainder Int.emod for a natural divisor. The fractional part is Mathlib's Int.fract, with Int.self_sub_floor, Int.floor_add_fract, Int.fract_nonneg, Int.fract_lt_one, Int.fract_add_intCast and Int.fract_eq_iff (proved in Mathlib).

Lean core has two remainders on the integers. Int.emod, written %, has 0≤a%b<∣b∣0 \le a \% b < |b| for b≠0b \ne 0 (Int.emod_nonneg, Int.emod_lt_of_pos), so it differs from a mod ba \bmod b when b<0b < 0. Int.fmod is the remainder of floor division (Int.fmod_def, Int.fmod_zero, Int.fmod_nonneg_of_pos, Int.fmod_lt_of_pos), and it is a mod ba \bmod b for all integers aa and bb; Int.fmod_eq_emod_of_nonneg says that the two agree for b≥0b \ge 0. The range for y<0y < 0, the characterization and the distributive law are not yet stated for TAOCP.kmod.

Notes and history

Gauss introduced congruences in 1801, and the remainder of a division is as old as arithmetic. The binary operation x mod yx \bmod y for all real arguments is younger. TAOCP 1.2.4 defines it by the formula of this section, with the convention x mod 0=xx \bmod 0 = x, and points out the distributive law; GKP94, Section 3.4, studies the operation further. The sign conventions of programming languages were settled one language at a time. Most hardware divides with truncation, so C and Java give the remainder the sign of the dividend, while Python, Haskell (mod) and Lean's Int.fmod follow the floor and the sign of the divisor, as this section does.

Sums of floors

Statement

A sum of floors looks hard, because the floor has no algebra of its own. This section evaluates four kinds of such sums in closed form. The methods are few. A sum can be checked to be periodic and then checked on one period. A floor can be written as a count, ⌊y⌋=∑1≤j≤y1\lfloor y \rfloor = \sum_{1 \le j \le y} 1 for y≥0y \ge 0, and the order of summation can be changed. A floor ⌊(a+b)/m⌋\lfloor (a + b)/m \rfloor with an integer aa can be split by the remainder of aa. The results are used throughout the analysis of algorithms: the splitting of nn into nearly equal parts is the recurrence of merge sort, and the sum of ⌊(nk+x)/m⌋\lfloor (nk + x)/m \rfloor is the tool for the analysis of hashing and of random number generators.

Theorem P2.4.59 (Hermite's identity). For each real xx and each integer n≥1n \ge 1,

∑0≤k<n⌊x+kn⌋=⌊nx⌋.\sum_{0 \le k < n} \Bigl\lfloor x + \frac{k}{n} \Bigr\rfloor = \lfloor nx \rfloor.

Lean: Int.sum_floor_add_div states ∑ i ∈ Finset.range n, ⌊x + i / n⌋ = ⌊n * x⌋ in each linearly ordered field with a floor (proved in Mathlib).

Proof. Let g(x)g(x) be the left side minus the right side. Replace xx by x+1nx + \frac{1}{n}. On the left, the term with index kk becomes the term with index k+1k + 1 for k<n−1k < n - 1, and the last term becomes ⌊x+1⌋=⌊x⌋+1\lfloor x + 1 \rfloor = \lfloor x \rfloor + 1, which is the old first term plus 11. So the left side grows by 11. On the right, ⌊nx+1⌋=⌊nx⌋+1\lfloor nx + 1 \rfloor = \lfloor nx \rfloor + 1, which also grows by 11. So g(x+1n)=g(x)g(x + \frac{1}{n}) = g(x), and gg has period 1n\frac{1}{n}. For 0≤x<1n0 \le x < \frac{1}{n}, each term x+knx + \frac{k}{n} lies in [0,1)[0, 1) and nxnx lies in [0,1)[0, 1), so all the floors are 00 and g(x)=0g(x) = 0. Each real xx differs from a number in [0,1n)[0, \frac{1}{n}) by an integer multiple of 1n\frac{1}{n}, namely 1n⌊nx⌋\frac{1}{n}\lfloor nx \rfloor. So g=0g = 0 everywhere. □\square

Example P2.4.60. For x=0.7x = 0.7 and n=3n = 3: ⌊0.7⌋+⌊1.03‾⌋+⌊1.36‾⌋=0+1+1=2\lfloor 0.7 \rfloor + \lfloor 1.0\overline{3} \rfloor + \lfloor 1.3\overline{6} \rfloor = 0 + 1 + 1 = 2, and ⌊2.1⌋=2\lfloor 2.1 \rfloor = 2. For n=2n = 2 the identity reads ⌊x⌋+⌊x+12⌋=⌊2x⌋\lfloor x \rfloor + \lfloor x + \frac{1}{2} \rfloor = \lfloor 2x \rfloor. So ⌊x+12⌋\lfloor x + \frac{1}{2} \rfloor, the integer nearest to xx (with ties rounded up), equals ⌊2x⌋−⌊x⌋\lfloor 2x \rfloor - \lfloor x \rfloor.

Theorem P2.4.61 (splitting into nearly equal parts). Let nn be an integer and m≥1m \ge 1 an integer. Then

n=∑0≤k<m⌊n+km⌋=∑0≤k<m⌈n−km⌉.n = \sum_{0 \le k < m} \Bigl\lfloor \frac{n + k}{m} \Bigr\rfloor = \sum_{0 \le k < m} \Bigl\lceil \frac{n - k}{m} \Bigr\rceil.

The mm parts ⌊(n+k)/m⌋\lfloor (n + k)/m \rfloor are in increasing order. Each part is ⌊n/m⌋\lfloor n/m \rfloor or ⌈n/m⌉\lceil n/m \rceil, and exactly n mod mn \bmod m of them are equal to ⌊n/m⌋+1\lfloor n/m \rfloor + 1.

Lean: Int.sum_range_add_ediv states ∑ i ∈ Finset.range n, (m + i) / n = m for 0 < n, with the integer division (proved in Mathlib).

Proof. Hermite's identity with x=n/mx = n/m and mm terms gives ∑0≤k<m⌊n/m+k/m⌋=⌊n⌋=n\sum_{0 \le k < m} \lfloor n/m + k/m \rfloor = \lfloor n \rfloor = n. For the ceilings, ⌈(n−k)/m⌉=−⌊(−n+k)/m⌋\lceil (n - k)/m \rceil = -\lfloor (-n + k)/m \rfloor by part 5 of the basic laws of Section 4.2, and the first form for −n-n gives the sum −(−n)=n-(-n) = n. For the parts, write n=qm+rn = qm + r with q=⌊n/m⌋q = \lfloor n/m \rfloor and 0≤r<m0 \le r < m. Then ⌊(n+k)/m⌋=q+⌊(r+k)/m⌋\lfloor (n + k)/m \rfloor = q + \lfloor (r + k)/m \rfloor, and 0≤r+k<2m0 \le r + k < 2m, so the part is q+[k≥m−r]q + [k \ge m - r]. The parts increase with kk, and exactly rr of the indices 0≤k<m0 \le k < m satisfy k≥m−rk \ge m - r. If r=0r = 0, all parts are q=n/mq = n/m; if r>0r > 0, the parts are qq and q+1=⌈n/m⌉q + 1 = \lceil n/m \rceil. □\square

Example P2.4.62. Split 1717 into 55 parts: ⌊17/5⌋=3\lfloor 17/5 \rfloor = 3, ⌊18/5⌋=3\lfloor 18/5 \rfloor = 3, ⌊19/5⌋=3\lfloor 19/5 \rfloor = 3, ⌊20/5⌋=4\lfloor 20/5 \rfloor = 4 and ⌊21/5⌋=4\lfloor 21/5 \rfloor = 4. The sum is 1717, and 17 mod 5=217 \bmod 5 = 2 of the parts are 44. For m=2m = 2 the theorem says ⌊n/2⌋+⌈n/2⌉=n\lfloor n/2 \rfloor + \lceil n/2 \rceil = n, the split of merge sort.

Theorem P2.4.63 (sum of square roots). For each integer n≥0n \ge 0, with a=⌊n⌋a = \lfloor \sqrt{n} \rfloor,

∑0≤k<n⌊k⌋=na−a(a+1)(2a+1)6=na−a33−a22−a6.\sum_{0 \le k < n} \lfloor \sqrt{k} \rfloor = na - \frac{a(a+1)(2a+1)}{6} = na - \frac{a^3}{3} - \frac{a^2}{2} - \frac{a}{6}.

Lean: the summand is Nat.sqrt k, by Real.floor_real_sqrt_eq_nat_sqrt (proved in Mathlib); the sum is not yet stated in Lean.

Proof. For k≥0k \ge 0, ⌊k⌋\lfloor \sqrt{k} \rfloor is the number of integers j≥1j \ge 1 with j≤kj \le \sqrt{k}, that is with j2≤kj^2 \le k. So

∑0≤k<n⌊k⌋=∑0≤k<n∑j≥1[j2≤k]=∑j≥1∑0≤k<n[j2≤k].\sum_{0 \le k < n} \lfloor \sqrt{k} \rfloor = \sum_{0 \le k < n} \sum_{j \ge 1} [j^2 \le k] = \sum_{j \ge 1} \sum_{0 \le k < n} [j^2 \le k].

The inner sum counts the integers kk with j2≤k<nj^2 \le k < n. There are n−j2n - j^2 of them when j2≤nj^2 \le n, that is when j≤aj \le a, and none when j>aj > a. So the sum is ∑1≤j≤a(n−j2)=na−a(a+1)(2a+1)6\sum_{1 \le j \le a} (n - j^2) = na - \frac{a(a+1)(2a+1)}{6}, by the sum of squares of Chapter 3. □\square

Example P2.4.64. For n=10n = 10, a=3a = 3. The terms ⌊k⌋\lfloor \sqrt{k} \rfloor for k=0,…,9k = 0, \ldots, 9 are 0,1,1,1,2,2,2,2,2,30, 1, 1, 1, 2, 2, 2, 2, 2, 3, with the sum 1616. The formula gives 30−3⋅4⋅76=30−14=1630 - \frac{3 \cdot 4 \cdot 7}{6} = 30 - 14 = 16.

Theorem P2.4.65 (sum of floors of a linear function). Let m≥1m \ge 1 and nn be integers, let xx be real, and let d=gcd⁡(m,n)d = \gcd(m, n). Then

∑0≤k<m⌊nk+xm⌋=d⌊xd⌋+(m−1)(n−1)2+d−12.\sum_{0 \le k < m} \Bigl\lfloor \frac{nk + x}{m} \Bigr\rfloor = d \Bigl\lfloor \frac{x}{d} \Bigr\rfloor + \frac{(m-1)(n-1)}{2} + \frac{d - 1}{2}.

Lean: not yet stated in Lean; the case n=1n = 1 is a case of Hermite's identity, Int.sum_floor_add_div (proved in Mathlib).

Proof. The proof uses two facts about the greatest common divisor from Chapter 5: m=dm′m = dm' and n=dn′n = dn' with gcd⁡(m′,n′)=1\gcd(m', n') = 1, and if m′m' divides n′jn'j then m′m' divides jj. By the second fact, the numbers n′k mod m′n'k \bmod m' for kk in a block of m′m' consecutive integers are distinct, so they are 0,1,…,m′−10, 1, \ldots, m' - 1 in some order. The range 0≤k<m0 \le k < m consists of dd such blocks. Since nk mod m=d (n′k mod m′)nk \bmod m = d\,(n'k \bmod m') by the distributive law of Section 4.3, the numbers nk mod mnk \bmod m for 0≤k<m0 \le k < m take each value djdj with 0≤j<m′0 \le j < m' exactly dd times.

Now write nk=m⌊nk/m⌋+nk mod mnk = m \lfloor nk/m \rfloor + nk \bmod m. Part 4 of the basic laws of Section 4.2 gives

⌊nk+xm⌋=⌊nkm⌋+⌊nk mod m+xm⌋.\Bigl\lfloor \frac{nk + x}{m} \Bigr\rfloor = \Bigl\lfloor \frac{nk}{m} \Bigr\rfloor + \Bigl\lfloor \frac{nk \bmod m + x}{m} \Bigr\rfloor.

Sum the first part over kk: ∑k⌊nk/m⌋=∑k(nk−nk mod m)/m=n(m−1)2−1m⋅d∑0≤j<m′dj=n(m−1)2−m−d2\sum_k \lfloor nk/m \rfloor = \sum_k (nk - nk \bmod m)/m = \frac{n(m-1)}{2} - \frac{1}{m} \cdot d \sum_{0 \le j < m'} dj = \frac{n(m-1)}{2} - \frac{m - d}{2}, since d2m′(m′−1)/(2m)=d(m′−1)/2=(m−d)/2d^2 m'(m'-1)/(2m) = d(m'-1)/2 = (m - d)/2. Sum the second part: it is d∑0≤j<m′⌊(dj+x)/m⌋=d∑0≤j<m′⌊x/m+j/m′⌋=d⌊m′x/m⌋=d⌊x/d⌋d \sum_{0 \le j < m'} \lfloor (dj + x)/m \rfloor = d \sum_{0 \le j < m'} \lfloor x/m + j/m' \rfloor = d \lfloor m'x/m \rfloor = d \lfloor x/d \rfloor, by Hermite's identity with m′m' terms. Add: d⌊x/d⌋+n(m−1)−m+d2d\lfloor x/d \rfloor + \frac{n(m-1) - m + d}{2}, and n(m−1)−m+d=(m−1)(n−1)+(d−1)n(m-1) - m + d = (m-1)(n-1) + (d - 1). □\square

Example P2.4.66. Take m=4m = 4, n=6n = 6 and x=1x = 1, so d=2d = 2. The terms are ⌊1/4⌋=0\lfloor 1/4 \rfloor = 0, ⌊7/4⌋=1\lfloor 7/4 \rfloor = 1, ⌊13/4⌋=3\lfloor 13/4 \rfloor = 3 and ⌊19/4⌋=4\lfloor 19/4 \rfloor = 4, with the sum 88. The formula gives 2⌊1/2⌋+3⋅52+12=0+8=82 \lfloor 1/2 \rfloor + \frac{3 \cdot 5}{2} + \frac{1}{2} = 0 + 8 = 8.

Corollary P2.4.67 (reciprocity law). For integers m,n≥1m, n \ge 1 and real xx,

∑0≤k<m⌊nk+xm⌋=∑0≤k<n⌊mk+xn⌋.\sum_{0 \le k < m} \Bigl\lfloor \frac{nk + x}{m} \Bigr\rfloor = \sum_{0 \le k < n} \Bigl\lfloor \frac{mk + x}{n} \Bigr\rfloor.

Lean: not yet stated in Lean.

Proof. The right side of the theorem does not change when mm and nn are exchanged, since gcd⁡(m,n)=gcd⁡(n,m)\gcd(m, n) = \gcd(n, m). □\square

Example P2.4.68. For m=3m = 3, n=5n = 5 and x=0x = 0, the left side is ⌊0⌋+⌊5/3⌋+⌊10/3⌋=0+1+3=4\lfloor 0 \rfloor + \lfloor 5/3 \rfloor + \lfloor 10/3 \rfloor = 0 + 1 + 3 = 4, and the right side is 0+0+1+1+2=40 + 0 + 1 + 1 + 2 = 4. The left side has 33 terms and the right side has 55, so the law can shorten a computation.

Proof

The four theorems use three methods. Hermite's identity uses periodicity: both sides jump by 11 at the same points, so their difference has period 1n\frac{1}{n} and is checked on one period. The sum of square roots uses a count: a floor is the number of integers below a bound, and the order of the double sum is changed, as in Chapter 1. The sum of a linear function uses the remainder: the integer part nknk of the numerator is split off, and what is left depends only on nk mod mnk \bmod m, whose values are known.

The reciprocity law has a picture. For x=0x = 0 the sum ∑0≤k<m⌊nk/m⌋\sum_{0 \le k < m} \lfloor nk/m \rfloor counts the points (k,j)(k, j) of the integer lattice with 0≤k<m0 \le k < m and 1≤j≤nk/m1 \le j \le nk/m, that is the points below the diagonal of the rectangle with the corners (0,0)(0, 0) and (m,n)(m, n). Turn the rectangle by a half turn, (k,j)↦(m−k,n−j)(k, j) \mapsto (m - k, n - j), and count the image points by rows instead of columns. The count becomes the sum for (n,m)(n, m). For x=0x = 0 the formula of the theorem is half the number (m−1)(n−1)(m-1)(n-1) of lattice points inside the rectangle, with a correction for the d−1d - 1 lattice points inside the diagonal.

Examples

Example P2.4.69. Merge sort sorts nn items by sorting ⌈n/2⌉\lceil n/2 \rceil and ⌊n/2⌋\lfloor n/2 \rfloor items and merging. The splitting theorem guarantees that the two halves have nn items together. A kk-way merge sort splits nn into the kk parts ⌊(n+j)/k⌋\lfloor (n + j)/k \rfloor for 0≤j<k0 \le j < k, and the theorem says that these parts differ by at most 11 and add up to nn. For n=100n = 100 and k=3k = 3 the parts are 33,33,3433, 33, 34.

Example P2.4.70. Hermite's identity gives a formula for the binary digits of nn. Apply the case n=2n = 2 to x=n/2j+1x = n/2^{j+1}: ⌊n/2j+1+12⌋=⌊n/2j⌋−⌊n/2j+1⌋\lfloor n/2^{j+1} + \frac{1}{2} \rfloor = \lfloor n/2^j \rfloor - \lfloor n/2^{j+1} \rfloor. Bit jj of nn is ⌊n/2j⌋−2⌊n/2j+1⌋\lfloor n/2^j \rfloor - 2\lfloor n/2^{j+1} \rfloor, the remainder of ⌊n/2j⌋\lfloor n/2^j \rfloor by 22. For n=13=11012n = 13 = 1101_2: ⌊13/1⌋−2⌊13/2⌋=13−12=1\lfloor 13/1 \rfloor - 2 \lfloor 13/2 \rfloor = 13 - 12 = 1, 6−6=06 - 6 = 0, 3−2=13 - 2 = 1 and 1−0=11 - 0 = 1.

Example P2.4.71. A linear congruential generator computes Xk+1=(aXk+c) mod mX_{k+1} = (aX_k + c) \bmod m. The theorem on linear sums gives the average of the integer parts ⌊(nk+x)/m⌋\lfloor (nk + x)/m \rfloor over a full period, and the sum ∑0≤k<m(nk mod m)=m(m−d)/2\sum_{0 \le k < m} (nk \bmod m) = m(m - d)/2 from its proof gives the average of the remainders. For m=12m = 12, n=5n = 5 and x=2x = 2, the sum ∑0≤k<12⌊(5k+2)/12⌋\sum_{0 \le k < 12} \lfloor (5k + 2)/12 \rfloor is 2+11⋅42+0=242 + \frac{11 \cdot 4}{2} + 0 = 24, which a direct count confirms.

Example P2.4.72. The sum of square roots grows like 23n3/2\frac{2}{3} n^{3/2}. For n=100n = 100, a=10a = 10, and the sum is 1000−385=6151000 - 385 = 615, while 23⋅1000=666.7\frac{2}{3} \cdot 1000 = 666.7. The difference is about n2\frac{n}{2}, as Exercise 10 shows.

Exercises

  1. [05] Split 100100 into 77 nearly equal parts with the formula of the splitting theorem.

  2. [M18] Prove that ∑k≥0⌊(n+2k)/2k+1⌋=n\sum_{k \ge 0} \lfloor (n + 2^k)/2^{k+1} \rfloor = n for each integer n≥0n \ge 0.

  3. [M15] Prove that ∑0≤k<n⌈k⌉=∑0≤k<n⌊k⌋+n−⌈n⌉\sum_{0 \le k < n} \lceil \sqrt{k} \rceil = \sum_{0 \le k < n} \lfloor \sqrt{k} \rfloor + n - \lceil \sqrt{n} \rceil for each integer n≥0n \ge 0.

  4. [M20] Let n≥0n \ge 0 and a=⌊n1/3⌋a = \lfloor n^{1/3} \rfloor. Prove that ∑0≤k<n⌊k1/3⌋=na−(a(a+1)2)2\sum_{0 \le k < n} \lfloor k^{1/3} \rfloor = na - \bigl(\frac{a(a+1)}{2}\bigr)^2.

  5. [M12] Evaluate ∑0≤k<12⌊(8k+5)/12⌋\sum_{0 \le k < 12} \lfloor (8k + 5)/12 \rfloor in two ways: by the theorem on linear sums, and term by term.

  6. [M15] Let m≥1m \ge 1 and nn be integers and d=gcd⁡(m,n)d = \gcd(m, n). Prove that exactly dd of the numbers nknk with 0≤k<m0 \le k < m are multiples of mm.

  7. [M18] Let τ(j)\tau(j) be the number of positive divisors of jj. Prove that ∑1≤k≤n⌊n/k⌋=∑1≤j≤nτ(j)\sum_{1 \le k \le n} \lfloor n/k \rfloor = \sum_{1 \le j \le n} \tau(j), and check it for n=10n = 10.

  8. [M22] Let m≥1m \ge 1 and nn be integers, xx real, and d=gcd⁡(m,n)d = \gcd(m, n). Prove that ∑0≤k<m⌈(nk+x)/m⌉=d⌈x/d⌉+(m−1)(n+1)2−d−12\sum_{0 \le k < m} \lceil (nk + x)/m \rceil = d\lceil x/d \rceil + \frac{(m-1)(n+1)}{2} - \frac{d-1}{2}.

  9. [M18] Let n≥1n \ge 1 be odd. Prove that ∑0≤k<n⌊x+2k/n⌋=⌊nx⌋+n−12\sum_{0 \le k < n} \lfloor x + 2k/n \rfloor = \lfloor nx \rfloor + \frac{n-1}{2} for each real xx.

  10. [M25] Let S(n)=∑0≤k<n⌊k⌋S(n) = \sum_{0 \le k < n} \lfloor \sqrt{k} \rfloor. Prove that S(n)=23n3/2−n2+R(n)S(n) = \frac{2}{3} n^{3/2} - \frac{n}{2} + R(n) with ∣R(n)∣≤n+16|R(n)| \le \frac{\sqrt{n} + 1}{6} for each n≥1n \ge 1.

Lean

Mathlib has Hermite's identity as Int.sum_floor_add_div and the splitting of an integer as Int.sum_range_add_ediv, both in Mathlib.Algebra.Order.Floor.BigOperators. The integer square root is Nat.sqrt, and Real.floor_real_sqrt_eq_nat_sqrt connects it to the real square root. These are proved in Mathlib. The remainder x mod yx \bmod y of Section 4.3 is TAOCP.kmod in TAOCP/Notation (tamnd/taocp-lean main).

The sum of ⌊k⌋\lfloor \sqrt{k} \rfloor, the sum of floors of a linear function and the reciprocity law are not yet stated in Lean. The first is a direct induction on nn with Nat.sqrt; the second needs the facts about Nat.gcd that Chapter 5 uses.

Notes and history

Hermite published his identity in 1884. The sum of ⌊(nk+x)/m⌋\lfloor (nk + x)/m \rfloor and its reciprocity law belong to the circle of ideas around Dedekind sums, which Dedekind introduced in the study of modular functions and which return in TAOCP's analysis of the serial correlation of linear congruential generators. The lattice point proof of reciprocity goes back to Eisenstein's proof of quadratic reciprocity, which counts lattice points under a diagonal in the same way. GKP94, Section 3.5, treats the same sums and the same reciprocity law.

The table lists the results of TAOCP 1.2.2 and 1.2.4 that this chapter proves. The numbers of the results are the numbers of this handbook.

TAOCP 1.2 Result of P2
1.2.2(1), 1.2.2(2) Definition P2.4.9, Examples P2.4.10 and P2.4.11
1.2.2(4), 1.2.2(5) Definition P2.4.1, Theorem P2.4.4
1.2.2(6) Definition P2.4.5, Theorem P2.4.8
1.2.2(7), 1.2.2(8) Definition P2.4.9, Theorem P2.4.12
1.2.2(9), 1.2.2(10) Definition P2.4.13, Example P2.4.15
1.2.2(11), 1.2.2(12) Theorem P2.4.16
1.2.2(13), 1.2.2(15) Definition P2.4.17
1.2.2(14) Theorem P2.4.20
1.2.2(16) to 1.2.2(19) Example P2.4.22
1.2.4, floor and ceiling Definition P2.4.27, Theorem P2.4.30
1.2.4(1), 1.2.4(3) Definition P2.4.42, Examples P2.4.43 and P2.4.44
1.2.4(2) Theorem P2.4.45
1.2.4(4) Definition P2.4.47
1.2.4, the distributive law Theorem P2.4.50
1.2.4, the floor sums of exercises 35 to 44 Theorems P2.4.59, P2.4.61, P2.4.63 and P2.4.65, Corollary P2.4.67

The analysis of an algorithm mixes two kinds of numbers. The input sizes, the counts of steps and the addresses in memory are integers. The formulas that describe them contain square roots, logarithms and quotients, and these are real numbers. This chapter gives the tools that move between the two kinds: the floor ⌊x⌋\lfloor x \rfloor and the ceiling ⌈x⌉\lceil x \rceil, which round a real number down or up to an integer, and the operation x mod yx \bmod y, which gives the remainder of a division.

Section 4.1 starts from the other end. It builds the power bxb^x for real xx in three steps, from integer to rational to real exponents, and checks at each step that the laws of exponents stay true. The logarithm is then the inverse of the power. The section ends with the rule that a positive integer nn has ⌊log⁡bn⌋+1\lfloor \log_b n \rfloor + 1 digits in radix bb, which is the first place where a floor and a logarithm meet.

Section 4.2 defines the floor and the ceiling and proves their basic laws. Its main tool is a rule for a continuous increasing function ff that takes integer values only at integers: then ⌊f(x)⌋=⌊f(⌊x⌋)⌋\lfloor f(x) \rfloor = \lfloor f(\lfloor x \rfloor) \rfloor. The rule gives the laws for floors of quotients and of roots in one step. The section ends with exact counts of the integers in an interval.

Section 4.3 defines x mod yx \bmod y for all real xx and yy, with the convention x mod 0=xx \bmod 0 = x. It finds the range of the remainder for positive and negative yy, proves the distributive law c(x mod y)=(cx) mod (cy)c(x \bmod y) = (cx) \bmod (cy), and relates the definition to the integer remainders of Lean and Mathlib, which differ from it for negative divisors. Section 4.4 evaluates sums of floors: Hermite's identity, the rule that splits an integer into nearly equal parts, the sum of ⌊k⌋\lfloor \sqrt{k} \rfloor, and the sum of ⌊(nk+x)/m⌋\lfloor (nk + x)/m \rfloor, whose value depends on the greatest common divisor of mm and nn.

Concrete Mathematics treats the same subjects in Sections 3.1 to 3.5, and the growth of the logarithm in Section 9.1. The results of TAOCP Sections 1.2.2 and 1.2.4 that this chapter proves are listed by number in the table at the end of Section 4.4.

The source of this page. Something is wrong? Report it.