TExcelTAD
Home › Foundations of Algorithm Analysis › Polynomials, Exponentials and Logarithms

Polynomials, Exponentials and Logarithms

A handful of functions appear in almost every running time: polynomials such as n2n^2, logarithms such as lg⁡n\lg n, exponentials such as 2n2^n, 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 OO, and why it matters a great deal in an exponent.

Notation used throughout the track: lg⁡n=log⁡2n\lg n = \log_2 n, ln⁡n=log⁡en\ln n = \log_e n, lg⁡kn=(lg⁡n)k\lg^k n = (\lg n)^k (a power, not repeated application), and lg⁡lg⁡n=lg⁡(lg⁡n)\lg \lg n = \lg(\lg n).

Monotone functions

A function ff is monotonically increasing if m≤nm \le n implies f(m)≤f(n)f(m) \le f(n), and strictly increasing if m<nm < n implies f(m)<f(n)f(m) < f(n); decreasing is defined the same way with the inequalities reversed. Running times are usually increasing, and that is what lets an analysis round nn up to the next power of two: if TT is increasing and n≤2k<2nn \le 2^k < 2n, then T(n)≤T(2k)T(n) \le T(2^k).

Floors and ceilings

For a real xx, ⌊x⌋\lfloor x \rfloor is the largest integer at most xx and ⌈x⌉\lceil x \rceil the smallest integer at least xx. They satisfy

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

and, for every integer nn and positive integers a,ba, b:

⌊n2⌋+⌈n2⌉=n,⌈⌈n/a⌉b⌉=⌈nab⌉,⌊⌊n/a⌋b⌋=⌊nab⌋\left\lfloor \frac{n}{2} \right\rfloor + \left\lceil \frac{n}{2} \right\rceil = n, \qquad \left\lceil \frac{\lceil n/a \rceil}{b} \right\rceil = \left\lceil \frac{n}{ab} \right\rceil, \qquad \left\lfloor \frac{\lfloor n/a \rfloor}{b} \right\rfloor = \left\lfloor \frac{n}{ab} \right\rfloor

The first one says that splitting nn 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 n/an/a for any integers, and -(-n // a) is the ceiling, with no floating point involved. The program checks all three identities, including negative nn:

python · try it yourself
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 aa and a positive integer nn, a mod n=a−n⌊a/n⌋a \bmod n = a - n \lfloor a/n \rfloor, the remainder in {0,1,…,n−1}\{0, 1, \ldots, n - 1\}. Python's % follows this definition even for negative aa, 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 dd is p(n)=∑i=0dainip(n) = \sum_{i=0}^{d} a_i n^i with ad≠0a_d \ne 0. When ad>0a_d > 0, p(n)=Θ(nd)p(n) = \Theta(n^d): the leading term wins, and the ratio p(n)/ndp(n)/n^d tends to ada_d. A function is polynomially bounded if it is O(nk)O(n^k) for some constant kk. 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 p(n)=3n3−100n2+7p(n) = 3n^3 - 100n^2 + 7, the value is negative until n=34n = 34, and the ratio approaches 3 slowly, because the correction term is −100/n-100/n.

Exponentials

For a>0a > 0 and real m,nm, n: a0=1a^0 = 1, am+n=amana^{m+n} = a^m a^n, (am)n=amn(a^m)^n = a^{mn} and a−1=1/aa^{-1} = 1/a. For a>1a > 1, ana^n is strictly increasing.

The key comparison is with polynomials: for all constants bb and a>1a > 1,

lim⁡n→∞nban=0,sonb=o(an)\lim_{n \to \infty} \frac{n^b}{a^n} = 0, \quad \text{so} \quad n^b = o(a^n)

(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: 1.1n1.1^n passes n10n^{10} for good only at n=686n = 686.

python · try it yourself
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 ee that come up in probabilistic analysis: 1+x≤ex1 + x \le e^x for every real xx, and (1+x/n)n→ex(1 + x/n)^n \to e^x as n→∞n \to \infty. The first gives bounds like (1−1/n)k≤e−k/n(1 - 1/n)^k \le e^{-k/n}, the typical way to estimate the chance that a bin stays empty in Balls into Bins, Coupons and Streaks.

Logarithms

For a,b,c>0a, b, c > 0 with bases not equal to 1:

log⁡c(ab)=log⁡ca+log⁡cblog⁡ban=nlog⁡balog⁡ba=log⁡calog⁡cbchange of basealog⁡bc=clog⁡baswap the outer and inner values\begin{aligned} \log_c(ab) &= \log_c a + \log_c b \\ \log_b a^n &= n \log_b a \\ \log_b a &= \frac{\log_c a}{\log_c b} && \text{change of base} \\ a^{\log_b c} &= c^{\log_b a} && \text{swap the outer and inner values} \end{aligned}

The last identity looks odd but has a one-line proof: take log⁡b\log_b of both sides and both become log⁡ba⋅log⁡bc\log_b a \cdot \log_b c. It is used every time a recursion tree has alog⁡bna^{\log_b n} leaves, which is the same as nlog⁡ban^{\log_b a}; see Recursion Trees.

Why the base doesn't matter in OO. By change of base, log⁡bn=lg⁡n/lg⁡b\log_b n = \lg n / \lg b, and 1/lg⁡b1/\lg b is a constant. So log⁡10n\log_{10} n, ln⁡n\ln n and lg⁡n\lg n all differ by constant factors and are all Θ(lg⁡n)\Theta(\lg n). That is why one writes O(nlog⁡n)O(n \log n) without a base.

Why the base does matter in an exponent. 2lg⁡n=n2^{\lg n} = n, but 4lg⁡n=(2lg⁡n)2=n24^{\lg n} = (2^{\lg n})^2 = n^2. Likewise nlog⁡23≈n1.585n^{\log_2 3} \approx n^{1.585} and nlog⁡43≈n0.792n^{\log_4 3} \approx n^{0.792} 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.

python · try it yourself
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 O(lg⁡kn)O(\lg^k n) for some constant kk. Every polylogarithm grows strictly slower than every positive power of nn:

lg⁡bn=o(na)for all constants b and a>0\lg^b n = o(n^a) \quad \text{for all constants } b \text{ and } a > 0

Substitute n=2mn = 2^m to see why: lg⁡bn=mb\lg^b n = m^b and na=(2a)mn^a = (2^a)^m, so this is the polynomial-versus-exponential fact again, in the variable mm. The ladder of standard growth rates, from slowest to fastest, is therefore

1≪lg⁡lg⁡n≪lg⁡n≪lg⁡2n≪n≪n≪nlg⁡n≪n2≪n3≪2n≪3n≪n!1 \ll \lg \lg n \ll \lg n \ll \lg^2 n \ll \sqrt{n} \ll n \ll n \lg n \ll n^2 \ll n^3 \ll 2^n \ll 3^n \ll n!

where f≪gf \ll g means f=o(g)f = o(g). Factorials and the even slower iterated logarithm come next, in Factorials, Stirling and the Iterated Logarithm.

Test yourself

Why can you write O(nlog⁡n)O(n \log n) without saying which base the logarithm has?

  1. Because all logarithms are equal
  2. Because log⁡bn=lg⁡n/lg⁡b\log_b n = \lg n / \lg b, and 1/lg⁡b1/\lg b is a constant factor
  3. Because computers use base 2
  4. Because the logarithm term is always negligible

Which equals 4lg⁡n4^{\lg n}?

  1. nn
  2. 2n2n
  3. n2n^2
  4. 4n4n

For integers nn, which expression always equals ⌈⌈n/3⌉/4⌉\lceil \lceil n/3 \rceil / 4 \rceil?

  1. ⌈n/12⌉\lceil n/12 \rceil
  2. ⌊n/12⌋\lfloor n/12 \rfloor
  3. ⌈n/7⌉\lceil n/7 \rceil
  4. ⌈n/3⌉/4\lceil n/3 \rceil / 4

Which statement is true for every constant bb?

  1. lg⁡bn=o(n0.01)\lg^b n = o(n^{0.01})
  2. lg⁡bn=Ω(n)\lg^b n = \Omega(\sqrt{n})
  3. nb=ω(1.01n)n^b = \omega(1.01^n)
  4. lg⁡bn=Θ(lg⁡n)\lg^b n = \Theta(\lg n)