The number of times you can halve n before reaching 1. That's why halving algorithms like binary search are O(log n). ```python def halvings(n): steps = 0 while n > 1: n //= 2 steps += 1 return steps
2¹⁰ ≈ 1,000 (1,024). 2²⁰ ≈ 1 million. 2³⁰ ≈ 1 billion. So log₂ of a billion is about 30. ```python print(2 ** 10, 2 ** 20) # → 1024 1048576 ```
5
New cards
[Math] Exponent rules
aᵐ · aⁿ = aᵐ⁺ⁿ. (aᵐ)ⁿ = aᵐⁿ. a⁰ = 1.
6
New cards
[Math] Logarithm rules
log(ab) = log a + log b. log(a/b) = log a − log b. log(aᵏ) = k · log a.
7
New cards
[Math] Change of base, and why log bases don't matter in Big-O
log_b(n) = log(n) / log(b). Changing the base only multiplies by a constant, so O(log₂ n) = O(log₁₀ n) = O(log n). ```python import math
for n in [10, 1000, 10 ** 6]: print(round(math.log2(n) / math.log10(n), 4)) # → 3.3219, then 3.3219, then 3.3219 ← always the same constant ```
8
New cards
[Math] Arithmetic series: 1 + 2 + 3 + ... + n
n(n + 1) / 2, which is O(n²). It's the cost of a nested loop where the inner loop runs i times. ```python n = 100 print(sum(range(1, n + 1)), n * (n + 1) // 2) # → 5050 5050
count = 0 for i in range(n): for j in range(i): count += 1 print(count) # → 4950 ← n(n − 1)/2 inner-loop runs, so O(n²) ```
9
New cards
[Math] Geometric series: 1 + 2 + 4 + ... + 2ᵏ
2ᵏ⁺¹ − 1. The last term is about half the total, which shows up in heap and tree analysis. ```python k = 10 print(sum(2 ** i for i in range(k + 1)), 2 ** (k + 1) - 1) # → 2047 2047 ```
10
New cards
[Math] Summation notation (Σ)
Σᵢ₌₁ⁿ i means "add i for every i from 1 to n," i.e., 1 + 2 + ... + n. ```python print(sum(i for i in range(1, 6))) # → 15 ← Σ i for i = 1..5 ```
11
New cards
[Math] Floor and ceiling
floor(x) rounds down, ceil(x) rounds up: floor(3.7) = 3, ceil(3.2) = 4. In Python, `//` is floor division and `math.ceil()` rounds up. ```python import math