Skip to main content

Sums, Logarithms and Factorials

12 min read

Analyzing algorithms needs only a small amount of mathematics, but it needs it constantly. This lesson collects the facts used throughout the course, explains where each one comes from, and shows how to use them. It can be read on its own or consulted when a lesson refers to it.

The Complexity Analysis lesson gives the most common summation formulas in a table; here we justify them and add a few more.

What you will learn in this lesson
  • How to manipulate summation notation and derive the closed forms of arithmetic and geometric series.
  • Why harmonic numbers Hn≈ln⁡nH_n \approx \ln n appear in algorithm analysis and how to bound sums with integrals.
  • The essential logarithm identities, floors and ceilings, and why the base matters inside an exponent.
  • How fast factorials grow via Stirling's approximation and why log⁡(n!)∈Θ(nlog⁡n)\log(n!) \in \Theta(n \log n).

Summation notation​

The expression

∑i=1nf(i)=f(1)+f(2)+⋯+f(n)\sum_{i=1}^{n} f(i) = f(1) + f(2) + \dots + f(n)

is read "the sum of f(i)f(i) for ii from 1 to nn". The variable ii is a dummy: ∑i=1ni\sum_{i=1}^{n} i and ∑j=1nj\sum_{j=1}^{n} j are the same number. Three rules are used all the time:

  • Constants come out: ∑i=1nc⋅f(i)=c⋅∑i=1nf(i)\sum_{i=1}^{n} c \cdot f(i) = c \cdot \sum_{i=1}^{n} f(i).
  • Sums split: ∑i=1n(f(i)+g(i))=∑i=1nf(i)+∑i=1ng(i)\sum_{i=1}^{n} (f(i) + g(i)) = \sum_{i=1}^{n} f(i) + \sum_{i=1}^{n} g(i).
  • Indices shift: ∑i=0n−1f(i+1)=∑i=1nf(i)\sum_{i=0}^{n-1} f(i+1) = \sum_{i=1}^{n} f(i), and summing in reverse order changes nothing: ∑i=0n−1(n−1−i)=∑i=0n−1i\sum_{i=0}^{n-1} (n - 1 - i) = \sum_{i=0}^{n-1} i.

A loop is a sum: the number of times the body of for (int i = 1; i <= n; i++) runs is ∑i=1n1=n\sum_{i=1}^{n} 1 = n. Nested loops become nested sums, which is how the complexity of loops is calculated.


Arithmetic series​

∑i=1ni=1+2+⋯+n=n(n+1)2\sum_{i=1}^{n} i = 1 + 2 + \dots + n = \frac{n(n+1)}{2}

Why. Write the sum twice, once forwards and once backwards, and add the columns:

S = 1 + 2 + ... + (n-1) + n
S = n + (n-1) + ... + 2 + 1
2S = (n+1) + (n+1) + ... + (n+1) + (n+1) = n (n+1)
Two copies of the staircase 1 + 2 + ... + n fit together into a rectangle of n by n + 1 squares1524334251n + 1 = 6 columnsblue: S = 1 + 2 + 3 + 4 + 5orange: S again, upside downtogether: 5 rows × 6 columns2S = n(n + 1) = 30S = n(n + 1)/2 = 15
The same trick in a picture: the staircase 1 + 2 + ... + n (blue) and a copy turned upside down (orange) fill a rectangle of n × (n + 1) squares, so each staircase has n(n + 1)/2.

Each of the nn columns adds up to n+1n + 1, so 2S=n(n+1)2S = n(n+1). (The story goes that Gauss found this trick as a schoolboy.)

Where it appears. In every triangular loop, where the inner loop runs 1,2,…,n1, 2, \dots, n times (or n−1,n−2,…,1n - 1, n - 2, \dots, 1): bubble sort, selection sort and insertion sort in its worst case all perform about n2/2n^2/2 comparisons. It is Θ(n2)\Theta(n^2): half of the square, but still quadratic.

More generally, any sum of terms that grow linearly is quadratic: ∑i=1n(a⋅i+b)=an(n+1)2+b⋅n∈Θ(n2)\sum_{i=1}^{n} (a \cdot i + b) = a \frac{n(n+1)}{2} + b \cdot n \in \Theta(n^2).

Sums of powers​

∑i=1ni2=n(n+1)(2n+1)6∑i=1ni3=(n(n+1)2)2\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6} \qquad \sum_{i=1}^{n} i^3 = \left(\frac{n(n+1)}{2}\right)^2

For example, 12+22+⋯+1002=338,3501^2 + 2^2 + \dots + 100^2 = 338{,}350. These formulas are proved by induction (see the next lesson). For analysis we usually only need their order:

∑i=1nik∈Θ(nk+1)\sum_{i=1}^{n} i^k \in \Theta(n^{k+1})

Why. Each of the nn terms is at most nkn^k, so the sum is at most n⋅nk=nk+1n \cdot n^k = n^{k+1}. And the largest half of the terms (those with i≥n/2i \ge n/2) are each at least (n/2)k(n/2)^k, so the sum is at least n2⋅(n2)k=nk+12k+1\frac{n}{2} \cdot \left(\frac{n}{2}\right)^k = \frac{n^{k+1}}{2^{k+1}}. Both bounds are a constant times nk+1n^{k+1}. This "upper bound by the largest term, lower bound by half of the terms" argument works for many sums.


Geometric series​

∑i=0kri=1+r+r2+⋯+rk=rk+1−1r−1(r≠1)\sum_{i=0}^{k} r^i = 1 + r + r^2 + \dots + r^k = \frac{r^{k+1} - 1}{r - 1} \qquad (r \ne 1)

Why. Call the sum SS. Then r⋅S=r+r2+⋯+rk+1r \cdot S = r + r^2 + \dots + r^{k+1}. Subtracting, almost every term cancels: r⋅S−S=rk+1−1r \cdot S - S = r^{k+1} - 1.

The important part is what happens for large kk, which depends only on the ratio rr:

RatioBehaviorOrder of the sumExample
r>1r \gt 1Each term is larger than all the previous ones together (for r≥2r \ge 2); the last term dominatesΘ(rk)\Theta(r^k)1+2+4+⋯+210=211−1=20471 + 2 + 4 + \dots + 2^{10} = 2^{11} - 1 = 2047
r=1r = 1All terms equalk+1k + 1
r<1r \lt 1The terms shrink; the first term dominates, and the sum never exceeds 11−r\frac{1}{1-r}Θ(1)\Theta(1)1+12+14+⋯<21 + \frac{1}{2} + \frac{1}{4} + \dots \lt 2
The geometric series 1 + 1/2 + 1/4 + ... never reaches 2: each term fills half of the remaining gap11/21/41/81/1620r < 1: the first term dominates, and the sum stays below 1/(1 − r) = 2
A geometric series with ratio 1/2. Each term covers half of the gap that remains up to 2, so the sum gets as close to 2 as we like but never reaches it.

This table is the heart of the analysis of divide and conquer. In the recursion tree of a division scheme, the work per level forms a geometric series of ratio a/bka / b^k, and the three cases of the formula are exactly the three rows above: the work is concentrated at the bottom, spread evenly, or concentrated at the top.

It also explains a fact that surprises many students: a for loop that doubles its variable (for (i = 1; i < n; i *= 2)) with a body of cost ii costs only 1+2+4+⋯+n/2<n1 + 2 + 4 + \dots + n/2 \lt n, which is O(n)O(n), not O(nlog⁡n)O(n \log n). The same argument explains why adding nn elements to an ArrayList costs O(n)O(n) in total: whenever the internal array is full, it is replaced by one larger by a constant factor (50% larger in Java) and the elements are copied, but the sizes of those copies form a geometric series, so all the copies together cost only a constant times the final size.


Harmonic numbers​

Hn=∑i=1n1i=1+12+13+⋯+1n≈ln⁡n+0.5772H_n = \sum_{i=1}^{n} \frac{1}{i} = 1 + \frac{1}{2} + \frac{1}{3} + \dots + \frac{1}{n} \approx \ln n + 0.5772

The harmonic series grows, but very slowly: H10≈2.93H_{10} \approx 2.93, H1000≈7.49H_{1000} \approx 7.49, H106≈14.39H_{10^6} \approx 14.39. The approximation ln⁡n+γ\ln n + \gamma (where γ≈0.5772\gamma \approx 0.5772 is the Euler-Mascheroni constant) is accurate to two decimals from n=100n = 100 on.

Where it appears. Whenever the probability of an event is 1/i1/i at step ii. The average number of comparisons of Quicksort with random pivots is about 2nln⁡n≈1.39 nlog⁡2n2 n \ln n \approx 1.39 \, n \log_2 n, and that ln⁡n\ln n comes from a harmonic number. It also appears in the number of times a running maximum changes when scanning a random array (about HnH_n times).

Bounding sums with integrals​

When ff is increasing, each term f(i)f(i) lies between the area under ff from i−1i - 1 to ii and the area from ii to i+1i + 1. Adding up,

∫0nf(x) dx  ≤  ∑i=1nf(i)  ≤  ∫1n+1f(x) dx\int_{0}^{n} f(x)\,dx \;\le\; \sum_{i=1}^{n} f(i) \;\le\; \int_{1}^{n+1} f(x)\,dx

(with the inequalities reversed for decreasing ff). For example, ∫1ndxx=ln⁡n\int_1^n \frac{dx}{x} = \ln n, which is why Hn≈ln⁡nH_n \approx \ln n. And ∑i=1nik≈∫0nxk dx=nk+1k+1\sum_{i=1}^{n} i^k \approx \int_0^n x^k \, dx = \frac{n^{k+1}}{k+1}, which agrees with Θ(nk+1)\Theta(n^{k+1}).

The harmonic sum as rectangles of height 1/i, compared with the area under 1/x12345678910y = 1/xrectangle i has width 1 and height 1/i,so the blue area is Hₙit lies above the curve from 1 to n + 1:Hₙ ≥ ln(n + 1), and Hₙ ≈ ln n + 0.57721
The harmonic sum as an area. Each rectangle has width 1 and height 1/i, and together they cover the area under y = 1/x, which is a logarithm.

Logarithms​

The logarithm is the inverse of the exponential:

log⁡bx=y  ⟺  by=x\log_b x = y \iff b^y = x

In algorithmics, log⁡2n\log_2 n answers a very concrete question: how many times can nn be halved before reaching 1? That is why it appears in every algorithm that discards half of the data at each step. Binary search on nn sorted elements makes at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 comparisons: 10 for n=1,000n = 1{,}000, 20 for n=106n = 10^6, 30 for n=109n = 10^9.

Rules​

RuleFormulaUse
Productlog⁡(xy)=log⁡x+log⁡y\log (x y) = \log x + \log ylog⁡(n!)=log⁡1+log⁡2+⋯+log⁡n\log (n!) = \log 1 + \log 2 + \dots + \log n
Quotientlog⁡(x/y)=log⁡x−log⁡y\log (x / y) = \log x - \log yLevels of a recursion tree: log⁡b(n/bi)=log⁡bn−i\log_b (n / b^i) = \log_b n - i
Powerlog⁡(xk)=klog⁡x\log (x^k) = k \log xlog⁡(n2)=2log⁡n\log (n^2) = 2 \log n, so O(log⁡n2)=O(log⁡n)O(\log n^2) = O(\log n)
Change of baselog⁡bx=log⁡cxlog⁡cb\log_b x = \dfrac{\log_c x}{\log_c b}All logarithms differ by a constant factor
Swapxlog⁡by=ylog⁡bxx^{\log_b y} = y^{\log_b x}Number of leaves of a recursion tree: alog⁡bn=nlog⁡baa^{\log_b n} = n^{\log_b a}
Inverseblog⁡bx=xb^{\log_b x} = x2log⁡2n=n2^{\log_2 n} = n

The change of base rule has an important consequence: log⁡2n=3.32⋅log⁡10n=1.44⋅ln⁡n\log_2 n = 3.32 \cdot \log_{10} n = 1.44 \cdot \ln n. Since constant factors are ignored in asymptotic notation, the base of the logarithm does not matter inside OO, and we simply write O(log⁡n)O(\log n). The base does matter inside an exponent: nlog⁡23≈n1.585n^{\log_2 3} \approx n^{1.585} and nlog⁡43≈n0.792n^{\log_4 3} \approx n^{0.792} are very different.

Logarithms grow slowly​

For every constant ε>0\varepsilon \gt 0, log⁡n∈O(nε)\log n \in O(n^{\varepsilon}): the logarithm grows more slowly than any power of nn, even n\sqrt{n} or n0.01n^{0.01} (although for n0.01n^{0.01} the difference only shows for astronomically large nn). Likewise, any power of a logarithm, (log⁡n)k(\log n)^k, grows more slowly than nn. This justifies the ordering O(log⁡n)⊂O(n)⊂O(n)⊂O(nlog⁡n)⊂O(n2)O(\log n) \subset O(\sqrt{n}) \subset O(n) \subset O(n \log n) \subset O(n^2) used in the growth rates lesson.

Floors and ceilings​

⌊x⌋\lfloor x \rfloor (floor) is the largest integer not greater than xx, and ⌈x⌉\lceil x \rceil (ceiling) the smallest integer not less than xx. They appear when sizes are divided: an array of nn elements is split into halves of ⌊n/2⌋\lfloor n/2 \rfloor and ⌈n/2⌉\lceil n/2 \rceil elements. In asymptotic analysis they can almost always be ignored: assuming nn is a power of 2 does not change the order of the result for the usual algorithms. Two exact facts worth remembering:

  • The number of bits needed to write n≥1n \ge 1 in binary is ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1.
  • The height of a complete binary tree (such as a heap) with nn nodes is ⌊log⁡2n⌋\lfloor \log_2 n \rfloor.

Factorials and Stirling's formula​

n!=1⋅2⋅3⋯nn! = 1 \cdot 2 \cdot 3 \cdots n

n!n! is the number of permutations of nn elements, so it measures exhaustive search over orderings: task assignment, the traveling salesman, and the number of possible orders a sorting algorithm must distinguish.

Stirling's formula gives its size:

n!≈2πn(ne)nn! \approx \sqrt{2 \pi n} \left( \frac{n}{e} \right)^n

It is surprisingly accurate: the relative error is about 1.7% for n=5n = 5, 0.8% for n=10n = 10 and 0.4% for n=20n = 20. Two consequences:

  • n!n! grows faster than any exponential cnc^n, because (n/e)n(n/e)^n has a base that itself grows. And n!≤nnn! \le n^n, so O(cn)⊂O(n!)⊂O(nn)O(c^n) \subset O(n!) \subset O(n^n).
  • log⁡2(n!)∈Θ(nlog⁡n)\log_2 (n!) \in \Theta(n \log n). Taking logarithms in Stirling's formula, log⁡2(n!)≈nlog⁡2n−1.44 n\log_2 (n!) \approx n \log_2 n - 1.44 \, n. For n=1000n = 1000: log⁡2(1000!)≈8,529\log_2 (1000!) \approx 8{,}529, while nlog⁡2n≈9,966n \log_2 n \approx 9{,}966.

The second fact is the core of the lower bound for comparison sorting: an algorithm that only compares elements must distinguish among n!n! possible orders, and each comparison has two outcomes, so it needs at least log⁡2(n!)∈Ω(nlog⁡n)\log_2 (n!) \in \Omega(n \log n) comparisons in the worst case.

A simpler argument gives the same order without Stirling: the largest half of the factors of n!n! are all at least n/2n/2, so n!≥(n/2)n/2n! \ge (n/2)^{n/2} and log⁡(n!)≥n2log⁡n2\log (n!) \ge \frac{n}{2} \log \frac{n}{2}, which is Ω(nlog⁡n)\Omega(n \log n); and n!≤nnn! \le n^n gives log⁡(n!)≤nlog⁡n\log (n!) \le n \log n.


Self-check​

1. Triangular loop. How many times does the body of for (i = 0; i < n; i++) for (j = i + 1; j < n; j++) run?

For each ii, the inner loop runs n−1−in - 1 - i times. The total is ∑i=0n−1(n−1−i)=0+1+⋯+(n−1)=n(n−1)2\sum_{i=0}^{n-1} (n - 1 - i) = 0 + 1 + \dots + (n-1) = \frac{n(n-1)}{2}, which is Θ(n2)\Theta(n^2). It is the number of pairs of different elements.

2. Doubling. A loop doubles i from 1 while i is less than n, and its body costs i operations. What is its total cost?

1+2+4+…1 + 2 + 4 + \dots up to less than nn: a geometric series of ratio 2, dominated by its last term, which is less than nn. The total is less than 2n2n: O(n)O(n).

3. Base of the logarithm. Is O(log⁡2n)=O(log⁡10n)O(\log_2 n) = O(\log_{10} n)? Is O(2log⁡2n)=O(2log⁡10n)O(2^{\log_2 n}) = O(2^{\log_{10} n})?

The first, yes: the two logarithms differ by a constant factor (3.32). The second, no: 2log⁡2n=n2^{\log_2 n} = n, while 2log⁡10n=nlog⁡102≈n0.3012^{\log_{10} n} = n^{\log_{10} 2} \approx n^{0.301}. A constant factor inside an exponent changes the order.

4. Halving. A recursive algorithm halves the problem until it has size 1. How many levels does the recursion have for n=106n = 10^6?

About log⁡2106≈20\log_2 10^6 \approx 20 levels (19.93, so 20 halvings with integer division).

5. Factorial. Order these functions from slowest to fastest growth: n!n!, 2n2^n, nnn^n, n10n^{10}, 10n10^n.

n10n^{10}, 2n2^n, 10n10^n, n!n!, nnn^n.

Common mistakes
  • Off-by-one errors in the limits of a sum. ∑i=0n−1\sum_{i=0}^{n-1} and ∑i=1n\sum_{i=1}^{n} have the same number of terms but different values.
  • Changing the base of a logarithm inside an exponent. 2log⁡4n=n2^{\log_4 n} = \sqrt{n}, not nn.
  • Confusing log⁡(n!)\log(n!) with log⁡n\log n. log⁡(n!)∈Θ(nlog⁡n)\log(n!) \in \Theta(n \log n).

Key ideas​

  • Arithmetic series: ∑i=n(n+1)/2∈Θ(n2)\sum i = n(n+1)/2 \in \Theta(n^2); in general ∑ik∈Θ(nk+1)\sum i^k \in \Theta(n^{k+1}).
  • Geometric series: dominated by the last term if r>1r \gt 1, by the first if r<1r \lt 1; all terms count equally if r=1r = 1. This is the key to divide and conquer.
  • Harmonic numbers: Hn≈ln⁡nH_n \approx \ln n; they appear in the average case of randomized algorithms such as Quicksort.
  • Logarithms: log⁡2n\log_2 n counts halvings; the base does not matter inside OO, but it does inside an exponent.
  • Factorials: n!≈2πn(n/e)nn! \approx \sqrt{2\pi n}(n/e)^n grows faster than any exponential, and log⁡(n!)∈Θ(nlog⁡n)\log (n!) \in \Theta(n \log n).
Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.