Polynomials, Exponentials and Logarithms
A handful of functions appear in almost every running time: polynomials such as , logarithms such as , exponentials such as , and the floors and ceilings that appear when an input is split in half. This lesson collects the facts about them that analyses rely on, checks the less obvious identities numerically, and explains two things that confuse many readers: why the base of a logarithm doesn't matter inside , and why it matters a great deal in an exponent.
Notation used throughout the track: , , (a power, not repeated application), and .
Monotone functions
A function is monotonically increasing if implies , and strictly increasing if implies ; decreasing is defined the same way with the inequalities reversed. Running times are usually increasing, and that is what lets an analysis round up to the next power of two: if is increasing and , then .
Floors and ceilings
For a real , is the largest integer at most and the smallest integer at least . They satisfy
and, for every integer and positive integers :
The first one says that splitting items into a floor half and a ceiling half loses nothing, which is exactly how merge sort divides its input. The nested identities say that halving twice, rounding each time, is the same as quartering once. In Python, n // a is the floor of for any integers, and -(-n // a) is the ceiling, with no floating point involved. The program checks all three identities, including negative :
def ceil_div(n, a):
return -(-n // a)
bad = 0
for n in range(-500, 501):
if n // 2 + ceil_div(n, 2) != n:
bad += 1
for a in range(1, 12):
for b in range(1, 12):
if ceil_div(ceil_div(n, a), b) != ceil_div(n, a * b):
bad += 1
if (n // a) // b != n // (a * b):
bad += 1
print("identity failures:", bad)
print("-7 // 2 =", -7 // 2, " ceil(-7/2) =", ceil_div(-7, 2), " -7 % 3 =", -7 % 3)identity failures: 0
-7 // 2 = -4 ceil(-7/2) = -3 -7 % 3 = 2
Modular arithmetic
For an integer and a positive integer , , the remainder in . Python's % follows this definition even for negative , which is why -7 % 3 prints 2 above. C, Java and JavaScript truncate toward zero instead and give -1. Hash functions and random-number generators are built on mod; when you port such code, check which convention the language uses.
Polynomials
A polynomial of degree is with . When , : the leading term wins, and the ratio tends to . A function is polynomially bounded if it is for some constant . Polynomially bounded running times are the working definition of "efficient" that the rest of the track uses.
Negative coefficients only delay the leading term. For , the value is negative until , and the ratio approaches 3 slowly, because the correction term is .
Exponentials
For and real : , , and . For , is strictly increasing.
The key comparison is with polynomials: for all constants and ,
(proved with L'Hopital's rule in Little o, Little omega and Limits). Every exponential with base above 1 eventually beats every polynomial. "Eventually" can be far away: passes for good only at .
p = lambda n: 3 * n**3 - 100 * n**2 + 7
first_positive = next(n for n in range(1, 1000) if p(n) > 0)
print("3n^3 - 100n^2 + 7 is first positive at n =", first_positive)
for n in [100, 1000, 10000]:
print(f" n = {n:>5}: p(n)/n^3 = {p(n) / n**3:.4f}")
last = max(n for n in range(2, 2000) if 1.1**n <= n**10)
print("1.1^n <= n^10 for the last time at n =", last)
for n in [100, last, last + 1]:
print(f" n = {n}: n^10 = {n**10:.3e}, 1.1^n = {1.1**n:.3e}")3n^3 - 100n^2 + 7 is first positive at n = 34
n = 100: p(n)/n^3 = 2.0000
n = 1000: p(n)/n^3 = 2.9000
n = 10000: p(n)/n^3 = 2.9900
1.1^n <= n^10 for the last time at n = 685
n = 100: n^10 = 1.000e+20, 1.1^n = 1.378e+04
n = 685: n^10 = 2.275e+28, 1.1^n = 2.259e+28
n = 686: n^10 = 2.308e+28, 1.1^n = 2.485e+28
Two facts about that come up in probabilistic analysis: for every real , and as . The first gives bounds like , the typical way to estimate the chance that a bin stays empty in Balls into Bins, Coupons and Streaks.
Logarithms
For with bases not equal to 1:
The last identity looks odd but has a one-line proof: take of both sides and both become . It is used every time a recursion tree has leaves, which is the same as ; see Recursion Trees.
Why the base doesn't matter in . By change of base, , and is a constant. So , and all differ by constant factors and are all . That is why one writes without a base.
Why the base does matter in an exponent. , but . Likewise and are different orders. Changing the base multiplies the exponent by a constant, and a constant factor in an exponent is not a constant factor in the value.
import math
for n in [10, 1000, 10**6]:
print(f"n = {n:>7}: lg n / log10 n = {math.log2(n) / math.log10(n):.4f}, lg n / ln n = {math.log2(n) / math.log(n):.4f}")
print("1 / log10(2) =", round(1 / math.log10(2), 4), " 1 / ln(2) =", round(1 / math.log(2), 4))
n = 1024
print("3^(lg n) =", 3 ** int(math.log2(n)), " n^(lg 3) =", round(n ** math.log2(3)))
print("4^(lg n) =", 4 ** int(math.log2(n)), " n^2 =", n**2)n = 10: lg n / log10 n = 3.3219, lg n / ln n = 1.4427
n = 1000: lg n / log10 n = 3.3219, lg n / ln n = 1.4427
n = 1000000: lg n / log10 n = 3.3219, lg n / ln n = 1.4427
1 / log10(2) = 3.3219 1 / ln(2) = 1.4427
3^(lg n) = 59049 n^(lg 3) = 59049
4^(lg n) = 1048576 n^2 = 1048576
Polylogarithms against polynomials
A function is polylogarithmically bounded if it is for some constant . Every polylogarithm grows strictly slower than every positive power of :
Substitute to see why: and , so this is the polynomial-versus-exponential fact again, in the variable . The ladder of standard growth rates, from slowest to fastest, is therefore
where means . Factorials and the even slower iterated logarithm come next, in Factorials, Stirling and the Iterated Logarithm.
Test yourself
Why can you write without saying which base the logarithm has?
- Because all logarithms are equal
- Because , and is a constant factor
- Because computers use base 2
- Because the logarithm term is always negligible
Which equals ?
For integers , which expression always equals ?
Which statement is true for every constant ?