This document covers a few mathematical constructs that appear very frequently when doing algorithmic analysis. We will spend only minimal time in class reviewing these concepts, so if you're unfamiliar with the following concepts, please be sure to read this document and head to office hours if you have any follow-up questions.
We also have a few practice problems located at the bottom of this doc if you're very rusty and want some practice.
(Note: this document was co-written with Meredith Wu)
Exponentiation is often thought of as repeated multiplication. For example, the expression \(b^x\) is equivalent to the following when \(x\) is an integer:
$$ b^x = \underbrace{b \cdot b \cdot b \cdots b}_{\text{$x$ times}} $$We say here that \(b\) is the base and that \(x\) is the exponent.
What happens if \(x\) is zero, or \(x\) is negative? In those cases, the exponent is defined to behave as follows:
(Note that neither the base nor the exponent needs to be integers. However, explaining how exponentiation works in these cases is beyond the scope of this document and isn't too relevant to this course.)
One example where exponents appear in code is if we had a loop that starts by performing operation then performs double that amount with each iteration. If we keep doubling the amount of work done, the code would be doing \(2^n\) operations on the \(n^\text{th}\) iteration.
Power of a power
Rule:
\(\displaystyle (b^x)^y = b^{xy} \)Example:
\(\displaystyle (b^2)^4 = (b \cdot b) \cdot (b \cdot b) \cdot (b \cdot b) \cdot (b \cdot b) = b^8 \)Multiplying exponents
Rule:
\(\displaystyle (b^x)\cdot(b^y) = b^{x+y} \)Example:
\(\displaystyle (b^2)\cdot(b^3) = (b \cdot b) \cdot (b \cdot b \cdot b) = b^5 \)Dividing exponents
Rule:
\(\displaystyle \frac{b^x}{b^y} = b^{x-y} \)Example:
\(\displaystyle \frac{b^5}{b^3} = \frac{b \cdot b \cdot b \cdot b \cdot b}{b \cdot b \cdot b} = b^2\) \(\displaystyle \frac{b^3}{b^5} = \frac{b \cdot b \cdot b}{b \cdot b \cdot b \cdot b \cdot b} = \frac{1}{b^2} = b^{-2} \)Taking the power of two multiplied terms
Rule:
\(\displaystyle (a \cdot b)^x = (a^x) \cdot (b^x) \)Example:
\(\displaystyle (a \cdot b)^2 = (a \cdot b) \cdot (a \cdot b) = (a \cdot a) \cdot (b \cdot b) = a^2 \cdot b^2 \)The logarithm operation is defined as the inverse of exponentials. For example, suppose we know \(b^x = n\); that is, we know that \(n\) is \(b\) to the power of \(x\). In that case, we also know \(x = \log_b(n)\); that is, we say that \(x\) is the logarithm of \(n\) with the base \(b\).
Here are some examples of equations using logs:
One important rule to keep in mind is that the logarithm is not defined when \(n = 0\). This makes sense when you remember that logarithms and exponents are inverses of each other: there are no possible values of \(b\) and \(x\) that can make \(b^x = 0\) true, which means that it is again impossible to find values of \(b\) and \(x\) that can make \(x = \log_b(0)\) true.
Because computers are binary systems, within this course, and within computer science in general, we treat the expression \(\log(n)\) and \(\log_2(n)\) as being exactly equivalent – that is, if we omit the base, we assume it's always equal to 2.
So, in the context of this course, \(\log_2(16) = \log(16) = 4\).
We will see many examples of the log operation in use when we begin analyzing trees and recursive functions later this quarter.
Note: we assume here that in all cases, variables are non-zero.
Log of a product
Rule:
\(\displaystyle \log_b(x\cdot y) = \log_b(x) + \log_b(y) \)Example:
\(\displaystyle \log_2(4 \cdot 8) = \log_2(32) = 5 = 2 + 3 = \log_2(4) + \log_2(8) \)Log of a fraction
Rule:
\(\displaystyle \log_b\left(\frac{x}{y}\right) = \log_b(x) - \log_b(y) \)Example:
\(\displaystyle \log_2\left(\frac{32}{4}\right) = \log_2(8) = 3 = 5 - 2 = \log_2(32) - \log_2(4) \)Log of a power
Rule:
\(\displaystyle \log_b(x^y) = y \cdot \log_b(x) \)Example:
\(\displaystyle \log_2(4^3) = \log_2(64) = 6 = 3 \cdot 2 = 3 \cdot \log_2(4) \)Power of a log
Rule:
\(\displaystyle x^{\log_b(y)} = y^{\log_b(x)} \)Example:
\(\displaystyle 4^{\log_2(8)} = 4^{3} = 4 \cdot 4 \cdot 4 = 8 \cdot 8 = 8^2 = 8^{\log_2(4)} \)Change of base
Rule:
\(\displaystyle \log_b(x) = \frac{\log_d(x)}{\log_d(b)} \)Example:
\(\displaystyle \log_2(9) = 3.16992... = \frac{\log_3(9)}{\log_3(2)} \)