# exponents + logs

> growth that multiplies, and the log that asks how many times.

source: https://learning.anthnydas.com/math/exponents-logs/

## tl;dr

an exponent counts how many times a number multiplies itself. a log reads that count back from the result.

- **product:** aᵐ · aⁿ = aᵐ⁺ⁿ (m factors, then n more)
- **power of a power:** (aᵐ)ⁿ = aᵐⁿ (m factors, n times over)
- **zero and negative:** a⁰ = 1, a⁻ⁿ = 1/aⁿ (what keeps the product rule true)
- **roots:** a^(1/n) = ⁿ√a (n of them multiply to a)
- **log:** log₂ 1024 = 10 (the exponent, read back)
- **products to sums:** log xy = log x + log y (the product rule, read backwards)
- **a sum:** log(a + b) (has no rule. neither does (a + b)ⁿ)
- **reach for it:** doubling, half-lives, a percent a year, reading O(log n) and O(2ⁿ), solving for an exponent

## the idea

an exponent counts multiplications. $2^{10}$ is ten factors of 2 multiplied together, and it comes to 1,024. that kind of growth is everywhere: a population that doubles, money that earns a percent of itself, a search that halves what's left. a log asks the question backwards. 1,024 is how many factors of 2? ten. $\log_2 1024 = 10$.

> **interactive figure:** an animation: 1 doubled ten times, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1,024, plotted as dots that hug the axis and then shoot up, with the curve y = 2ˣ drawn through them. then the log reads 1,024 back: across to the curve and down to 10 doublings, log₂ 1024 = 10. then a square view where y = 2ˣ folds over the dashed line y = x onto y = log₂ x, and the points (3, 8) and (8, 3) read one fact both ways: 2³ = 8 and log₂ 8 = 3.

start at 1 and double ten times: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1,024. the first five doublings reach 32. the next five reach 1,024.

each step is twice the one before, so the curve $y = 2^x$ bends up and keeps bending. that's growth that multiplies.

the log asks the other question. how many doublings make 1,024? read across from 1,024 to the curve, then down: ten. $\log_2 1024 = 10$.

swap the question and the axes swap. $y = \log_2 x$ is $y = 2^x$ folded over the line $y = x$.

one fact, read both ways: $2^3 = 8$ on the cyan curve, $\log_2 8 = 3$ on the amber one.

## explore

change the base. with $b > 1$, $y = b^x$ climbs faster and faster, and $y = \log_b x$ climbs slower and slower, and the two stay mirror images across $y = x$. drag the point along $b^x$: its mirror on $\log_b x$ has the same two numbers the other way round. the base can't be 1, since $1^x$ is 1 for every $x$ and there would be no exponent to read back.

> **interactive figure:** an interactive chart with equal axes from −2 to 8: y = bˣ in cyan, y = log_b x in amber and y = x dashed between them. a slider sets the base b from 1.2 to 4, starting at 2. a point drags along bˣ, starting at x = 1.5, where 2^1.5 = 2.83, and its mirror on the log curve reads log₂ 2.83 = 1.5. the arrow keys move it too.

## try it

> **interactive figure:** a practice exercise with six kinds of question: a power like 2⁵, an exponent rule applied to numbers, a log read back like log₂ 64, which two whole numbers a log sits between, a log rule, and growth that multiplies: doublings, half-lives and a percent a year.

## the rules

### exponents

every rule comes from counting factors. $a^n$ is $n$ copies of $a$ multiplied together.

$$
a^m \cdot a^n = a^{m+n}
$$

$m$ factors of $a$, then $n$ more: $m + n$ factors in all.

$$
\frac{a^m}{a^n} = a^{m-n}
$$

$n$ of the $m$ factors on top cancel against the $n$ below.

$$
(a^m)^n = a^{mn}
$$

a group of $m$ factors, written out $n$ times.

$$
(ab)^n = a^n b^n
$$

$n$ copies of $ab$, with the $a$'s and the $b$'s sorted into two groups.

$$
a^0 = 1 \qquad a^{-n} = \frac{1}{a^n}
$$

not a count of factors any more, but the only values that keep the product rule true. $a^n \cdot a^0$ has to equal $a^n$ and so $a^0 = 1$. and $a^n \cdot a^{-n}$ has to equal $a^0 = 1$ too, which makes $a^{-n}$ the reciprocal $1 / a^n$.

$$
a^{1/n} = \sqrt[n]{a} \qquad a^{m/n} = \left(\sqrt[n]{a}\right)^m
$$

the power of a power again: $(a^{1/n})^n = a^1$, so $a^{1/n}$ is the number whose $n$th power is $a$. $8^{2/3} = (\sqrt[3]{8})^2 = 4$.

### growth that multiplies

$$
y = a \cdot b^x
$$

$a$ is the amount at the start and $b$ the factor each step. $b = 2$ doubles. $b = 1/2$ halves, and the time one halving takes is the half-life. a rise of $r$ percent a step is $b = 1 + r/100$: 3% a year is $b = 1.03$, and 24 years of it is $1.03^{24} \approx 2.03$, a little more than double.

### e

split a year's 100% interest into $n$ payments of $100/n$ percent each, and one unit of money grows to $(1 + 1/n)^n$ by the end of the year. paid once, that's 2. twice, 2.25. monthly, 2.6130. daily, 2.7146. the number it closes in on is $e \approx 2.718$, and growth that compounds at every instant is $e^x$. it's the natural base for growth, and the derivatives page says why.

> **interactive figure:** a chart of one unit of money over a year at 100% interest, paid n times: a cyan staircase under the dashed curve eˣ. a slider picks n from 1, 2, 4, 12, 52 and 365 payments a year, starting at 12. the staircase ends at 2, 2.25, 2.4414, 2.6130, 2.6926 and 2.7146, closing in on e = 2.7183.

### logs

$$
\log_b x = y \iff b^y = x
$$

the log is the exponent. $\log_2 1024 = 10$ because $2^{10} = 1024$. $\log_{10} 1000 = 3$. $\ln e = 1$, where $\ln$ is $\log_e$. in every base, $\log_b 1 = 0$, since $b^0 = 1$, and $\log_b b = 1$.

$$
\log_b (xy) = \log_b x + \log_b y
$$

write $x = b^m$ and $y = b^n$. then $xy = b^{m+n}$ by the product rule, and the log reads that exponent back: $m + n$.

$$
\log_b \frac{x}{y} = \log_b x - \log_b y
$$

the quotient rule, read the same way: $x / y = b^{m-n}$.

$$
\log_b (x^n) = n \log_b x
$$

$x^n = (b^m)^n = b^{mn}$: the power of a power.

$$
\log_b x = \frac{\log_c x}{\log_c b}
$$

change of base. any log is any other log divided by a constant, so a calculator with only $\ln$ or $\log_{10}$ does every base: $\log_2 10 = \ln 10 / \ln 2 \approx 3.32$.

## worked examples

### how many doublings

a culture of 100 cells doubles every hour. after $t$ hours it's $100 \cdot 2^t$: 819,200 cells after 13 hours and 1,638,400 after 14. when does it pass a million?

$$
100 \cdot 2^t = 1{,}000{,}000 \quad\Rightarrow\quad 2^t = 10{,}000 \quad\Rightarrow\quad t = \log_2 10000 \approx 13.29
$$

divide both sides by the start, then read the exponent off with a log.

### a half-life

a sample of 80 g has a half-life of 3 days. after $t$ days there's $80 \cdot (1/2)^{t/3}$ left: 9 days is three half-lives, $80 / 8 = 10$ g, and 10 days is $80 \cdot (1/2)^{10/3} \approx 7.94$ g. when is 5 g left? $80 / 16 = 5$, so after four half-lives: 12 days.

### a percent a year

3% a year is a factor of 1.03 a year. how long until it doubles?

$$
1.03^t = 2 \quad\Rightarrow\quad t = \log_{1.03} 2 = \frac{\ln 2}{\ln 1.03} \approx 23.4 \text{ years}
$$

the rule of 70 gets close without a calculator: $70 / 3 \approx 23.3$. it works because $\ln 2 \approx 0.69$ and $\ln(1 + r) \approx r$ for a small $r$. at 7% a year it says 10 years, against 10.2 from the log.

### solving for the exponent

$2^x = 10$. the answer sits between 3 and 4, since $2^3 = 8$ and $2^4 = 16$. the log gives it exactly:

$$
x = \log_2 10 = \frac{\ln 10}{\ln 2} \approx 3.32
$$

check: $2^{3.32} \approx 9.99$.

### why log n is small

$\log_2 1{,}000{,}000 \approx 19.93$. so a sorted list of a million items takes about 20 halvings to get down to one item, and that's what [binary search](/algos/binary-search/) does: 20 looks, where a scan can take a million. a thousand items take 10 halvings and a billion take 30. every $\times 1{,}000$ in $n$ adds about 10, because $\log_2 1000 \approx 9.97$. it's why O(log n) sits next to O(1) in [big o](/algos/big-o/).

> **interactive figure:** a chart of log₂ n against n from 0 to 1,000,000 on plain axes: the curve shoots up at the left and then barely climbs, 9.97 at a thousand and 19.93 at a million.

### change of base in javascript

`Math.log` is $\ln$. dividing two of them does any base, but the division goes through floating point, and the answer can land a hair off a whole number. `Math.log10` and `Math.log2` give the whole number.[^mdn-log]

```js
Math.log(1000) / Math.log(10); // 2.9999999999999996
Math.log10(1000); // 3
Math.log2(1024); // 10
Math.log(Math.E); // 1
Math.log(0); // -Infinity
Math.log(-1); // NaN
64 ** (1 / 3); // 3.9999999999999996
Math.cbrt(64); // 4
```

python's `math.log(x, base)` is the same division, and its docs point to `log10` and `log2` as the more accurate ones for those bases.[^py-log]

## the key idea up close

### a log scale

put each number $x$ at distance $\log x$ from the start of a line. then 1, 10, 100, 1,000 are evenly spaced, and so are 1, 2, 4, 8. the distance from $a$ to $ab$ is

$$
\log(ab) - \log a = \log b
$$

the same length wherever $a$ is. multiplying by $b$ is a step of a fixed size, so on a log scale, every step multiplies. that's the whole trick of a slide rule: lay $\log a$ and $\log b$ end to end, and the mark under the end reads $ab$.

> **interactive figure:** two number lines. the top one is plain, 0 to 1,000,000, where a = 100, b = 1,000 and a · b = 100,000 all sit in the left tenth. the bottom one is a log scale, 1 to 1,000,000 with a tick at every power of ten, where a sits at 2, b at 3, and b's length laid after a's lands on a · b at 2 + 3 = 5. drag a or b along the log scale, snapping to the marks 1 to 9 of each decade, or move them with the arrow keys.

### the log rules are the exponent rules read backwards

a log is an exponent, so every log rule is an exponent rule with the roles swapped. write $x = b^m$ and $y = b^n$, so that $m = \log_b x$ and $n = \log_b y$:

$$
\begin{aligned}
xy &= b^m b^n = b^{m+n} &&\Rightarrow& \log_b(xy) &= m + n \\[2pt]
\frac{x}{y} &= b^{m-n} &&\Rightarrow& \log_b\frac{x}{y} &= m - n \\[2pt]
x^k &= (b^m)^k = b^{mk} &&\Rightarrow& \log_b(x^k) &= km
\end{aligned}
$$

change of base comes from the power rule in the other base. take $\log_c$ of both sides of $x = b^m$:

$$
\log_c x = m \log_c b \quad\Rightarrow\quad m = \frac{\log_c x}{\log_c b}
$$

the left side is $\log_b x$. so every log is every other log times a constant, which is why big o drops the base: $\log_2 n$ is about $3.32 \log_{10} n$ for every $n$.

## pitfalls

- **a sum has no rule.** $\log(a + b)$ isn't $\log a + \log b$: $\log_{10}(10 + 100) = \log_{10} 110 \approx 2.04$, not $1 + 2$. and $(a + b)^n$ isn't $a^n + b^n$: $(2 + 3)^2 = 25$, not $4 + 9$. the rules are about products and quotients
- **$-2^2$ is $-4$.** the exponent binds before the minus, so it's $-(2^2)$. $(-2)^2$ is 4. python reads `-2 ** 2` as $-4$ too. javascript refuses it as a syntax error and makes you write the parentheses either way[^mdn-exp]
- **no log of 0 or of a negative number,** in the real numbers. $b^y$ is positive for every real $y$, so no exponent reaches 0 or goes below it. `Math.log(0)` is `-Infinity` and `Math.log(-1)` is `NaN`. and no base 1: $1^y$ is always 1
- **which log is "log".** $\log_{10}$ on a calculator, $\ln$ in most math, $\log_2$ in computer science. in javascript `Math.log` is $\ln$. they differ by a constant factor, so it never matters inside a big o
- **a power can lead for a while.** $n^3$ is bigger than $2^n$ for every $n$ from 2 to 9: 729 against 512 at $n = 9$. at $n = 10$ it's 1,000 against 1,024, and $2^n$ never looks back. $n^{10}$ holds out until $n = 59$. growth that multiplies passes every power in the end, which is why O(2ⁿ) is the last row of the table on the [big o](/algos/big-o/) page
- **the float in the root.** `64 ** (1 / 3)` is 3.9999999999999996: `1 / 3` is stored a hair under a third, so the power lands a hair under 4. `Math.cbrt` and `Math.sqrt` give 4 and 2 exactly

## practice

1. simplify $\frac{2^7 \cdot 2^3}{2^4}$.

   **answer:** add the exponents on top and subtract the one below: $2^{7 + 3 - 4} = 2^6 = 64$.

2. read each log back as an exponent.

   $$
   \log_3 81 \qquad \log_{10} 0.001 \qquad \log_2 \frac{1}{8}
   $$

   **answer:** each one asks for an exponent. $3^4 = 81$, so $\log_3 81 = 4$. $10^{-3} = 0.001$, so $\log_{10} 0.001 = -3$. $2^{-3} = 1/8$, so $\log_2 (1/8) = -3$.

3. work out $8^{2/3}$ and $16^{-1/2}$.

   **answer:** root first, then power: $8^{2/3} = (\sqrt[3]{8})^2 = 2^2 = 4$. the negative exponent divides: $16^{-1/2} = 1 / \sqrt{16} = 1/4$.

4. a sample of 200 g has a half-life of 5 hours. how much is left after 15 hours, and when is 12.5 g left?

   **answer:** 15 hours is three half-lives: $200 \to 100 \to 50 \to 25$, so 25 g. 12.5 g is one more halving, four in all: 20 hours.

5. solve $5^x = 100$.

   **answer:** take a log of both sides, in any base: $x = \log_5 100 = \frac{\ln 100}{\ln 5} \approx 2.86$. check it against the powers around it: $5^2 = 25$ and $5^3 = 125$, so $x$ between 2 and 3 fits.

6. a sorted list holds 4 billion items. how many times does binary search halve it, at most, before one item is left?

   **answer:** $\log_2 4{,}000{,}000{,}000 \approx 31.9$, so 32 halvings. $2^{32} = 4{,}294{,}967{,}296$ is the first power of 2 past 4 billion. a million took 20, and the extra factor of 4,000 cost about 12 more.

[^mdn-log]: mdn's [Math.log](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log), [Math.log10](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10) and [Math.log2](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2) reference pages: `Math.log` is the natural logarithm, base e, `Math.log10` and `Math.log2` are the base 10 and base 2 logarithms, and for another base mdn says to divide `Math.log(x)` by `Math.log(otherBase)`.

[^py-log]: python's [math](https://docs.python.org/3/library/math.html#math.log) docs: with two arguments, `math.log(x, base)` is calculated as `log(x)/log(base)`, and `math.log10` and `math.log2` are usually more accurate than `log(x, 10)` and `log(x, 2)`.

[^mdn-exp]: mdn's [exponentiation operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation) reference page: a unary operator right before the base, as in `-2 ** 2`, is a `SyntaxError` in javascript, so the expression can't be read two ways. the same expression is −4 in python and 4 in bash.

## related

- https://learning.anthnydas.com/math/functions.md
- https://learning.anthnydas.com/math/trig.md
- https://learning.anthnydas.com/math/derivatives.md
- https://learning.anthnydas.com/math/series.md
- https://learning.anthnydas.com/algos/big-o.md
- https://learning.anthnydas.com/algos/binary-search.md
