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.
Powers and logarithms
Statement
This section defines for a real number and a real exponent . 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 .
Definition P2.4.1 (integer powers). Let be a number and an integer. For the power is when and when . For and the power is . The number is the base and is the exponent. In particular .
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. , , and . A negative base with an integer exponent is no problem: the sign of is .
Example P2.4.3. The value is a convention, and it is the convention that keeps formulas short. With it, a polynomial has the value at , and the binomial theorem holds also for . The power for has no value.
Theorem P2.4.4 (laws of exponents). Let and be numbers and let and be integers. Then
for all when , and for when or 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 . The first law holds for , and , so it holds for all by induction. The second law follows from the first by induction on : . The third law uses the commutative law: . Now let . The definition gives for every integer , also for . For the first law with and , the natural case gives , so . 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 .
Definition P2.4.5 (rational powers). Let be real and let be rational with integers and . The power is the unique real with . In particular is the positive th root of .
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 is the th root (proved in Mathlib).
Example P2.4.6. , because and . Also , because . The fraction need not be in lowest terms: is the positive with , and this is again .
Example P2.4.7. The definition needs . For the fraction suggests the value , since . The fraction has the same value, and it asks for with , which gives for or . No single choice gives a function of the rational number that keeps all the laws of exponents.
Theorem P2.4.8 (laws of rational exponents). The power is well defined: there is exactly one with , and depends only on the number . For and rational and ,
For and rational , .
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 and , implies , by induction on . So there is at most one with for a given . For existence, let . The set contains and has the upper bound , so it has a least upper bound . If , a number slightly larger than is still in ; if , a number slightly smaller is still an upper bound. Both contradict the choice of , so . (The words "slightly larger" use the bound for , which follows from the binomial theorem.) Now let , so . If and , then , so . So the power depends only on the number .
For the laws, write and with one denominator. Then by the integer laws, so is the positive th root of , which is . 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 . Then , since a positive has . So when , and .
Definition P2.4.9 (real powers). Let and let be real. The power is the least upper bound of the set . For let , and for let .
Lean: Real.rpow, with Real.rpow_def_of_pos ( for , 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 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 is an integer with , then for each rational . For rational the least upper bound is itself, by the monotonicity in the theorem above, so the new definition agrees with the old one. For , the rationals give , to five decimal places. The true value is .
Example P2.4.11. The rationals and give and , to six decimal places. Since is increasing, the real number with lies between and . This number is , defined below.
Theorem P2.4.12 (laws of real exponents). Let and let and be real. Then , and . For the function is strictly increasing and continuous, it tends to as and to as , and so it is a bijection from 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 . The key fact is that as . Write with . Bernoulli's inequality for , which follows by induction on , gives , so .
Let be real and let be rationals with . Then . So the least upper bound of the with equals the greatest lower bound of the with . The product law follows: for rationals and , , so ; the same argument with rationals above and gives . For , choose rationals ; then . For continuity at , , and for this is at most , which tends to . For the limits, , and . The intermediate value theorem now shows that each is a value . The law holds for rational by the rational laws and continuity in ; for real it follows by continuity in . The law for follows in the same way. The cases and reduce to by the definition.
Definition P2.4.13 (logarithm). Let with , and let . The logarithm of to the base is the unique real number with .
Lean: Real.logb b y, defined as ; Real.rpow_logb states (proved in Mathlib).
Example P2.4.14. , , and because . A logarithm is an exponent: the question "" is the question "to which power must be raised to give ?".
Example P2.4.15. From it follows that , so . In the same way gives . To five places, .
Theorem P2.4.16 (laws of logarithms). Let with , let and let be real. Then
For the function is strictly increasing and continuous on the positive reals; for 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 and . Then , so . Also , so . The values at and come from and . The inverse of a strictly increasing continuous bijection from onto the positive reals is strictly increasing and continuous. For , .
Definition P2.4.17 (binary and natural logarithms). The binary logarithm is . The number is , which is also the limit of . The natural logarithm is . The common logarithm is .
Lean: Real.log is the natural logarithm, and Real.logb 2 is the binary logarithm; Real.exp 1 is , with the bounds Real.exp_one_gt_d9 and Real.exp_one_lt_d9 (proved in Mathlib).
Example P2.4.18. counts the halvings: , and , so a million can be halved times and stay at least , and the th halving takes it below . Binary search in a sorted table of a million entries needs about comparisons for this reason.
Example P2.4.19. and , to five places. The natural logarithm is the one with the simplest derivative, , and its value is close to the harmonic number of Chapter 3.
Theorem P2.4.20 (change of base). Let with , and let . Then
In particular , and .
Lean: Real.log_div_log () (proved in Mathlib).
Proof. Let and . Then , so , and . Here because . For the last claim take .
Theorem P2.4.21 (number of digits). Let and be integers. The representation of in radix with a nonzero leading digit has exactly digits, where is the unique integer with . So , where is the largest integer and is the smallest integer (Section 4.2).
Lean: Nat.length_digits states (b.digits n).length = Nat.log b n + 1 for and ; Nat.log b n is , 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 digits in radix is with . If , it is at least . It is at most . So , and there is only one such because the intervals are disjoint. Take logarithms: , which says . For the ceiling, means , and means . So , which says .
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 , from Bernoulli's inequality, shows that the rational powers below and the rational powers above squeeze one number between them, and so gives the real powers with their continuity.
A shorter route, the one Mathlib takes, defines first as an integral or as the inverse of the exponential series, and then sets . The laws of exponents then follow from the single law . The route of this section needs no series and no integrals, and it shows why for irrational is the number that a computer approximates when it uses rational exponents.
Examples
Example P2.4.22. The binary digits of come from repeated squaring. Since , , and lies in . For a number in write in binary. Then , so exactly when . In that case replace by , whose binary logarithm is ; otherwise replace by . Start with :
| step | bit | new | |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 |
So , and the eight bits give . The true value is , and the next bits 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 has decimal digits. With , the product is , so has digits. Its leading digit comes from the fractional part: and , so the leading digit is . Indeed .
Example P2.4.24. To solve , take logarithms: . So , and has binary digits: . The constant converts decimal digits to bits: a number with decimal digits has about bits.
Example P2.4.25. The change of base shows that all logarithms are proportional. For each , . So a running time of steps is the same as steps, and statements about the growth of a logarithm, such as , do not depend on the base. This is the reason that the base of a logarithm is often left out inside -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 with , and suggests . Within this section all bases are positive and all exponents real, and no such trouble arises.
Exercises
-
[10] Find , , and .
-
[12] Let , , and let be real. Prove that .
-
[M18] Prove that is irrational.
-
[M15] Which is larger, or ? Decide with the value , and check that the error in that value cannot change the answer.
-
[M20] How many decimal digits does have, and what is its leading digit? Use .
-
[M20] Let have binary digits. Prove that has or binary digits, and give an example of each case.
-
[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 .
-
[HM22] Prove that as . (Hint: write and use one term of the binomial theorem.)
-
[M12] From and , find and 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 for ; 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 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 to Int.log. All of these are proved in Mathlib.
The construction of 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 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 took their modern form with Euler, who also gave 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 for 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 with .
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 there are integers below and integers above , 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 . 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 be real. The floor is the largest integer that is at most . The ceiling is the smallest integer that is at least .
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 and .
Example P2.4.28. and . For negative numbers the floor is not "drop the digits after the point": , because , and . For an integer both are the number itself: .
Example P2.4.29. Since , lies between and , so and . Also , and .
Theorem P2.4.30 (basic laws). Let and be real and let be an integer.
- if and only if , if and only if . Also if and only if , if and only if .
- .
- ; ; ; .
- and .
- and .
- .
- If , then and .
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: means and , since is the next integer. The second form moves to the other side. The ceiling is the mirror case. Part 2 collects the inequalities of part 1. For part 3, if then is one of the integers at most , so ; conversely . The second claim of part 3 is the negation of the first, and the other two are the mirror images. For part 4, is an integer with , so it is by part 1. For part 5, gives , so ; replace by for the second law. For part 6, if is an integer both sides are . If not, , so by part 1. For part 7, , so by part 3.
Theorem P2.4.31 (continuous increasing functions). Let be a continuous and strictly increasing function on an interval of real numbers. Suppose that is an integer only when is an integer. Then for each with ,
and for each with , .
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 is an integer there is nothing to prove. Otherwise , so , and part 7 of the basic laws gives . Suppose the inequality is strict, and let . Then , and gives by part 3. By the intermediate value theorem there is with and . By the assumption, is an integer. But there is no integer in the interval , since the next integer after is larger than . This contradiction proves the law for floors. The law for ceilings has the mirror proof.
Example P2.4.32. The condition on integer values cannot be left out. Let and . Then but . The function takes the integer value at the point , which is not an integer.
Corollary P2.4.33 (nested floors). Let be real, let be an integer and let be a positive integer. Then
In particular for each real , and so for each real and each positive integer .
Lean: Int.floor_div_natCast states , where the division on the right is the integer division Int.ediv (proved in Mathlib).
Proof. The function is continuous and strictly increasing on . If is an integer, then is an integer. The theorem applies on . For the last claim take and .
Theorem P2.4.34 (integer quotients). Let be an integer and a positive integer. Then is the unique integer with and . Also
Lean: Rat.floor_intCast_div_natCast states with the integer division on the right; Int.sub_floor_div_mul_nonneg and Int.sub_floor_div_mul_lt give (proved in Mathlib).
Proof. Let and . The bracket , multiplied by , gives , that is . If also with , then , so by part 1 of the basic laws. For the ceiling, let be an integer. Then holds if and only if , by part 3. For integers this is the same as , which is the same as , that is . By part 3 this holds if and only if . Two integers with the same set of upper bounds among the integers are equal. The last form is part 4 of the basic laws: .
Theorem P2.4.35 (floors of roots). Let be an integer.
- For each integer , is the unique integer with .
- For each real , and .
Lean: for , Real.floor_real_sqrt_eq_nat_sqrt states Nat.sqrt n, and Nat.sqrt_le' and Nat.lt_succ_sqrt' give for Nat.sqrt n (proved in Mathlib).
Proof. For part 1, let . Then , and the th power is increasing on the nonnegative reals, so . Conversely, an integer with satisfies , so it is the floor. For part 2, apply the theorem on continuous increasing functions to on . If is an integer, then is an integer.
Theorem P2.4.36 (counting integers in an interval). Let be real. The number of integers in the interval
and, when , the number in is . For integers and , the number of multiples of among is .
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 satisfies if and only if , and if and only if . So the integers in are the integers from to . There are of them. This number is not negative, since . In the same way, if and only if , and if and only if . These give the other three counts; the condition for the open interval excludes the case , where the formula gives . The multiples of in are with , and there are of them.
Example P2.4.37. The interval contains integers, namely and . The numbers contain multiples of . By inclusion and exclusion, they contain numbers that are divisible by or by .
Proof
Each proof of this section has the same plan. A statement about is replaced by a statement about an integer and the bracket , and the floor sign disappears. Part 3 of the basic laws is the tool for this. It says that "" and "" are the same statement when is an integer. Two integers are equal when they have the same integer upper bounds, so a claim follows from " for each integer ". The proof of the formula for 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 and .
Examples
Example P2.4.38. The integer square root by Newton's method. Let and . Start with any integer , for example , and put . Stop at the first with . Then . For the proof, first note that by the corollary on nested floors, applied to the real number , the integer and the divisor . By the inequality of the arithmetic and geometric means, , so at every step. If , then , so and , which gives . So the sequence decreases while it is above , and it cannot go below . For the values are , and the next value is , so . Check: .
Example P2.4.39. Binary search in a table of items looks at the middle position of the current range . A range of items splits into parts of and items around the middle. By the corollary on nested floors, , and after halvings at most items are left. So the search ends after at most comparisons, the number of binary digits of (Section 4.1).
Example P2.4.40. The year of the Gregorian calendar is a leap year when , except when and . By the theorem on counting, the number of leap years among the years is . For this is . Such counts occur in each formula for the day of the week.
Example P2.4.41. The integers in number , and this count is additive: for the counts for and add up to the count for . 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
-
[05] Find , and .
-
[10] Prove that for each real .
-
[M12] Prove that is or , and that is or . Give examples of all four cases.
-
[M15] Prove that if , and otherwise.
-
[10] A disk stores data in blocks of bytes, numbered from . Prove that a file of bytes needs blocks, and that byte number of the file, counted from , lies in block .
-
[M15] Prove or disprove, for all real : (a) ; (b) .
-
[M15] How many of the integers are perfect squares or perfect cubes?
-
[M20] Let be real and an integer. Prove that the number of integers with is .
-
[M15] Prove that for each real .
-
[M22] Find all real with .
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 and 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 and were introduced by Kenneth Iverson in 1962, in his language APL. Before that the floor was written , after Gauss, and the bracket is still common in number theory. The sign 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 carried out with integer arithmetic only.
The mod operation
Statement
Division of an integer by a positive integer gives a quotient and a remainder. This section defines the remainder for all real numbers and , with no condition on the signs and with a value also for . 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 , and the distributive law moves freely between integers and reals.
Definition P2.4.42 (mod). Let and be real. If , then
If , then . The number is the quotient and is the remainder. The operation binds like multiplication: means .
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 . Since Lean defines , the one formula already gives the value for .
Example P2.4.43. For integers: , , and . The remainder has the sign of the divisor , not the sign of .
Example P2.4.44. For reals: , and . Also and for each , for example .
Theorem P2.4.45 (range of the remainder). Let and be real. Then is an integer multiple of , and
In particular when , and .
Lean: TAOCP.floor_mul_add_kmod states ; for the range is Int.sub_floor_div_mul_nonneg and Int.sub_floor_div_mul_lt (proved in Mathlib). The range for is not yet stated for TAOCP.kmod.
Proof. The first claim is the definition. Let , so . If , multiply by : . If , multiplication by reverses the inequalities: . If , then and .
Theorem P2.4.46 (characterization of the remainder). Let , and let be the interval if and if . For each real the remainder is the unique such that is an integer multiple of . As a consequence, for real , and each integer :
- ;
- ;
- if and only if is an integer multiple of ;
- if and only if 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, has the two properties. Let with and for integers and . Then . Both and lie in an interval of length that is open at one end, so , and . So and . For part 1, is in and is a multiple of , so it is the remainder of . Part 2 is the case where is replaced by , which differs from by a multiple of . For part 3, if , part 1 gives the equality. Conversely, if the remainders are equal, then is a difference of two multiples of . Part 4 is part 3 with .
Definition P2.4.47 (fractional part). The fractional part of a real number is . It is often written , when no confusion with a set is possible. So and .
Lean: Mathlib Int.fract x, with Int.self_sub_floor ( 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. , while . The fractional part of a negative number is not the digits after the point. For each that is not an integer, .
Example P2.4.49. The fractional parts of for are , , and , to five places. Since is irrational, no two of the numbers are equal: from with it would follow that 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 , and ,
Lean: for integers and , Int.mul_fmod_mul_of_pos (proved in Lean core); for TAOCP.kmod the law is not yet stated.
Proof. If , both sides are , since and . If , both sides are : the left side is , and the right side is . If and , then , so .
Example P2.4.51. The law turns a remainder of decimals into a remainder of integers. Take : , so . With the law gives , so the remainders for a negative divisor follow from the remainders for a positive one: .
Theorem P2.4.52 (integer remainders). Let and be integers. Then is an integer. If , then and are the quotient and the remainder of the division of by with . For all , including , 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 ; Int.fmod_def states a.fmod b = a - b * a.fdiv b, Int.fmod_zero gives the value for , and Int.fmod_eq_emod_of_nonneg and Int.emod_def relate the two remainders for (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 is an integer, so is an integer. For this is the theorem on integer quotients of Section 4.2. Lean's Int.fdiv a b is defined as the floor of for and as for , and Int.fmod a b is Int.fdiv a b, which is the definition of this section in both cases.
Example P2.4.53. Programming languages differ for negative operands. For the pairs , the operation of this section gives the remainders . Lean's Int.emod (the operator % on Int, also used by Mathlib) gives : its remainder is never negative. The truncating remainder Int.tmod, which is the % of C and Java, gives : its remainder has the sign of . All three agree when and .
Proposition P2.4.54 (sums and nested remainders). Let , and be real.
- .
- For each nonzero integer , .
Lean: for integers, Int.add_emod, Int.emod_emod_of_dvd and Int.fmod_fmod_of_dvd (proved in Lean core).
Proof. If , both parts say and . Let . For part 1, and are integer multiples of , so the two numbers and differ by a multiple of . Part 3 of the characterization gives equal remainders. For part 2, is an integer multiple of , so it is an integer multiple of ; part 3 of the characterization again gives the claim.
Proof
The definition of is one line, and each law of this section follows from the bracket of Section 4.2. The theorem on the range multiplies the bracket by ; the characterization adds uniqueness; the distributive law notes that the quotient does not change when both numbers are multiplied by . The value is the one that keeps the distributive law true for and for , and it agrees with Lean's convention .
The characterization is the form to use in proofs. It says that a remainder is determined by two facts: the difference is a multiple of , and lies in the half open interval . 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 , and hours after the hour it shows ; with , the clock face shows in the place of . The days of the week work in the same way: since , the day 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 in radians reduces to the interval as . For , and . A program that computes for large makes this reduction first, and the rounding error of multiplied by the quotient sets a limit to its accuracy.
Example P2.4.57. A memory address must often be a multiple of . The smallest multiple of that is at least is , and the gap to it is , by Exercise 9. For , , so , and the aligned address is .
Example P2.4.58. The algorithm of Euclid replaces the pair by until the second number is . For the remainders are , , and , so the last nonzero number is . Chapter 5 proves that it is the greatest common divisor, and Exercise 7 shows that the remainders shrink fast.
Exercises
-
[05] Find , , , and .
-
[08] Find , and .
-
[M12] Let . Prove that is when is an integer and otherwise.
-
[08] Prove that for all real and .
-
[M15] Prove that for all integers , and . Show by an example that the rule fails for some real and with .
-
[10] Use the distributive law to find .
-
[M15] Let . Prove that .
-
[M12] The sawtooth function is when is not an integer, and when is an integer. Prove that and for each real .
-
[M10] Let . Prove that for each real .
-
[M10] Prove that for all real and .
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 (), TAOCP.floor_mul_add_kmod () 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 for (Int.emod_nonneg, Int.emod_lt_of_pos), so it differs from when . 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 for all integers and ; Int.fmod_eq_emod_of_nonneg says that the two agree for . The range for , 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 for all real arguments is younger. TAOCP 1.2.4 defines it by the formula of this section, with the convention , 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, for , and the order of summation can be changed. A floor with an integer can be split by the remainder of . The results are used throughout the analysis of algorithms: the splitting of into nearly equal parts is the recurrence of merge sort, and the sum of is the tool for the analysis of hashing and of random number generators.
Theorem P2.4.59 (Hermite's identity). For each real and each integer ,
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 be the left side minus the right side. Replace by . On the left, the term with index becomes the term with index for , and the last term becomes , which is the old first term plus . So the left side grows by . On the right, , which also grows by . So , and has period . For , each term lies in and lies in , so all the floors are and . Each real differs from a number in by an integer multiple of , namely . So everywhere.
Example P2.4.60. For and : , and . For the identity reads . So , the integer nearest to (with ties rounded up), equals .
Theorem P2.4.61 (splitting into nearly equal parts). Let be an integer and an integer. Then
The parts are in increasing order. Each part is or , and exactly of them are equal to .
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 and terms gives . For the ceilings, by part 5 of the basic laws of Section 4.2, and the first form for gives the sum . For the parts, write with and . Then , and , so the part is . The parts increase with , and exactly of the indices satisfy . If , all parts are ; if , the parts are and .
Example P2.4.62. Split into parts: , , , and . The sum is , and of the parts are . For the theorem says , the split of merge sort.
Theorem P2.4.63 (sum of square roots). For each integer , with ,
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 , is the number of integers with , that is with . So
The inner sum counts the integers with . There are of them when , that is when , and none when . So the sum is , by the sum of squares of Chapter 3.
Example P2.4.64. For , . The terms for are , with the sum . The formula gives .
Theorem P2.4.65 (sum of floors of a linear function). Let and be integers, let be real, and let . Then
Lean: not yet stated in Lean; the case 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: and with , and if divides then divides . By the second fact, the numbers for in a block of consecutive integers are distinct, so they are in some order. The range consists of such blocks. Since by the distributive law of Section 4.3, the numbers for take each value with exactly times.
Now write . Part 4 of the basic laws of Section 4.2 gives
Sum the first part over : , since . Sum the second part: it is , by Hermite's identity with terms. Add: , and .
Example P2.4.66. Take , and , so . The terms are , , and , with the sum . The formula gives .
Corollary P2.4.67 (reciprocity law). For integers and real ,
Lean: not yet stated in Lean.
Proof. The right side of the theorem does not change when and are exchanged, since .
Example P2.4.68. For , and , the left side is , and the right side is . The left side has terms and the right side has , so the law can shorten a computation.
Proof
The four theorems use three methods. Hermite's identity uses periodicity: both sides jump by at the same points, so their difference has period 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 of the numerator is split off, and what is left depends only on , whose values are known.
The reciprocity law has a picture. For the sum counts the points of the integer lattice with and , that is the points below the diagonal of the rectangle with the corners and . Turn the rectangle by a half turn, , and count the image points by rows instead of columns. The count becomes the sum for . For the formula of the theorem is half the number of lattice points inside the rectangle, with a correction for the lattice points inside the diagonal.
Examples
Example P2.4.69. Merge sort sorts items by sorting and items and merging. The splitting theorem guarantees that the two halves have items together. A -way merge sort splits into the parts for , and the theorem says that these parts differ by at most and add up to . For and the parts are .
Example P2.4.70. Hermite's identity gives a formula for the binary digits of . Apply the case to : . Bit of is , the remainder of by . For : , , and .
Example P2.4.71. A linear congruential generator computes . The theorem on linear sums gives the average of the integer parts over a full period, and the sum from its proof gives the average of the remainders. For , and , the sum is , which a direct count confirms.
Example P2.4.72. The sum of square roots grows like . For , , and the sum is , while . The difference is about , as Exercise 10 shows.
Exercises
-
[05] Split into nearly equal parts with the formula of the splitting theorem.
-
[M18] Prove that for each integer .
-
[M15] Prove that for each integer .
-
[M20] Let and . Prove that .
-
[M12] Evaluate in two ways: by the theorem on linear sums, and term by term.
-
[M15] Let and be integers and . Prove that exactly of the numbers with are multiples of .
-
[M18] Let be the number of positive divisors of . Prove that , and check it for .
-
[M22] Let and be integers, real, and . Prove that .
-
[M18] Let be odd. Prove that for each real .
-
[M25] Let . Prove that with for each .
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 of Section 4.3 is TAOCP.kmod in TAOCP/Notation (tamnd/taocp-lean main).
The sum of , 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 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 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 and the ceiling , which round a real number down or up to an integer, and the operation , which gives the remainder of a division.
Section 4.1 starts from the other end. It builds the power for real 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 has digits in radix , 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 that takes integer values only at integers: then . 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 for all real and , with the convention . It finds the range of the remainder for positive and negative , proves the distributive law , 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 , and the sum of , whose value depends on the greatest common divisor of and .
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.