TExcelTAD
Home › Basic Math & Pre-Algebra › Growth Rates and Big-O

Growth Rates and Big-O

Some formulas grow slowly and some explode. When a program handles nn items, the number of steps it takes might grow like log⁡n\log n, nn, n2n^2, or 2n2^n, and for large nn that difference decides whether it finishes in a millisecond or never. This lesson compares these growth rates with tables, counts the steps of real loops, and introduces Big-O notation, the shorthand programmers use for them.

The usual suspects

These are the functions that come up again and again. log⁡2n\log_2 n is the logarithm base 2: how many times you can halve nn before reaching 1.

Name Formula Doubling nn does this to the work
constant 11 nothing
logarithmic log⁡2n\log_2 n adds 1 step
linear nn doubles it
linearithmic nlog⁡2nn \log_2 n slightly more than doubles it
quadratic n2n^2 multiplies it by 4
exponential 2n2^n squares it

A table makes the difference concrete:

python · try it yourself
import math

print(f"{'n':>6} {'log2 n':>7} {'n log2 n':>10} {'n²':>12} {'2ⁿ':>24}")
for n in [1, 10, 20, 50, 100, 1000]:
    two_n = f"{2**n:.3e}" if n > 60 else f"{2**n:,}"
    print(f"{n:>6} {math.log2(n):>7.2f} {n * math.log2(n):>10.0f} {n*n:>12,} {two_n:>24}")
     n  log2 n   n log2 n           n²                       2ⁿ
     1    0.00          0            1                        2
    10    3.32         33          100                    1,024
    20    4.32         86          400                1,048,576
    50    5.64        282        2,500    1,125,899,906,842,624
   100    6.64        664       10,000                1.268e+30
  1000    9.97       9966    1,000,000               1.072e+301

At a billion simple steps per second, n2n^2 for n=100 000n = 100\,000 takes 10 seconds. 2n2^n for n=100n = 100 would take about 4×10134 \times 10^{13} years, thousands of times the age of the universe. log⁡2\log_2 of a billion is only about 30.

Counting the steps of code

The growth rate comes from the shape of the code, not the speed of the machine:

  • one loop over nn items → about nn steps
  • a loop inside a loop, both over nn → about n⋅n=n2n \cdot n = n^2 steps
  • halving the problem each step (binary search) → about log⁡2n\log_2 n steps
  • trying every subset of nn items → 2n2^n steps
python · try it yourself
def linear(n):
    steps = 0
    for i in range(n):
        steps += 1
    return steps

def all_pairs(n):
    steps = 0
    for i in range(n):
        for j in range(n):
            steps += 1
    return steps

def halving(n):
    steps = 0
    while n > 1:
        n //= 2
        steps += 1
    return steps

for n in [8, 16, 32, 64]:
    print(f"n={n:>3}  linear={linear(n):>3}  pairs={all_pairs(n):>5}  halving={halving(n)}")
n=  8  linear=  8  pairs=   64  halving=3
n= 16  linear= 16  pairs=  256  halving=4
n= 32  linear= 32  pairs= 1024  halving=5
n= 64  linear= 64  pairs= 4096  halving=6

Each time nn doubles, the linear count doubles, the pair count quadruples, and the halving count goes up by just one.

Big-O: keep only the fastest-growing term

Suppose a function takes exactly 3n2+20n+5003n^2 + 20n + 500 steps. For small nn the 500 dominates. But as nn grows, the n2n^2 term takes over and the rest becomes noise:

n=10:300+200+500=1 000(n2 part: 30%)n=1 000:3 000 000+20 000+500(n2 part: 99.3%)\begin{aligned} n = 10{:} &\quad 300 + 200 + 500 = 1\,000 && (n^2 \text{ part: } 30\%) \\ n = 1\,000{:} &\quad 3\,000\,000 + 20\,000 + 500 && (n^2 \text{ part: } 99.3\%) \end{aligned}

Big-O notation keeps only the dominant term and drops its constant factor: 3n2+20n+5003n^2 + 20n + 500 is O(n2)O(n^2). It describes how the cost grows, not its exact size. Two common rules:

  • drop constant factors: 5n5n and n/2n/2 are both O(n)O(n)
  • drop smaller terms: n2+nlog⁡nn^2 + n \log n is O(n2)O(n^2)

The ranking, slowest-growing first: O(1)<O(log⁡n)<O(n)<O(nlog⁡n)<O(n2)<O(2n)O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n).

A picture shows how quickly the curves separate (the y-axis is cut off at 400):

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

n = np.linspace(1, 20, 200)
plt.figure(figsize=(6, 4))
for y, label in [(np.log2(n), "log₂ n"), (n, "n"), (n * np.log2(n), "n log₂ n"),
                 (n**2, "n²"), (2**n, "2ⁿ")]:
    plt.plot(n, y, label=label)
plt.ylim(0, 400)
plt.xlabel("n (input size)")
plt.ylabel("steps")
plt.legend()
plt.title("Growth rates")
print("2^n passes n^2 for good after n =", next(k for k in range(5, 50) if 2**k > k**2) - 1)
2^n passes n^2 for good after n = 4

In software

  • Searching. Scanning a list is O(n)O(n). Binary search on sorted data is O(log⁡n)O(\log n): about 30 steps for a billion items. A hash table lookup is O(1)O(1) on average.
  • Sorting. Bubble sort is O(n2)O(n^2). Merge sort and Python's built-in sorted are O(nlog⁡n)O(n \log n). For a million items that is about 2×1072 \times 10^7 steps instead of 101210^{12}.
  • Nested loops over the same data (comparing every user with every other user) are the classic accidental O(n2)O(n^2). They work on test data and time out in production.
  • Brute-forcing passwords is exponential in the length, which is why every extra character makes a password far stronger (see Password Policy).
  • LLM attention compares every token with every other token, which is O(n2)O(n^2) in the context length. That is why long contexts are expensive.

The Time Complexity lesson in the DSA course takes this further.

Exercises

Exercise 1: Doubling

An O(n2)O(n^2) job takes 3 seconds for 10 000 items. Roughly how long for 40 000 items? And for an O(nlog⁡n)O(n \log n) job that takes 3 seconds for 10 000 items?

Answer

About 48 s for O(n2)O(n^2); about 13.8 s for O(nlog⁡n)O(n \log n).

Solution
O(n2): n grows ×4→work grows ×42=×16→3⋅16=48 sO(nlog⁡n): ratio=40000⋅log⁡24000010000⋅log⁡210000=4⋅15.2913.29≈4.60→3⋅4.60≈13.8 s\begin{aligned} &O(n^2){:}\ n \text{ grows } \times 4 \to \text{work grows } \times 4^2 = \times 16 \to 3 \cdot 16 = 48\ \text{s} \\ &O(n \log n){:}\ \text{ratio} = \frac{40000 \cdot \log_2 40000}{10000 \cdot \log_2 10000} \\ &\qquad = \frac{4 \cdot 15.29}{13.29} \approx 4.60 \to 3 \cdot 4.60 \approx 13.8\ \text{s} \end{aligned}
python · try it yourself
import math
r = (40000 * math.log2(40000)) / (10000 * math.log2(10000))
print(round(r, 2), round(3 * r, 1))
4.6 13.8

Exercise 2: Simplify to Big-O

Write each in Big-O: (a) 7n+127n + 12, (b) n2/2+100nn^2/2 + 100n, (c) 5log⁡2n+35 \log_2 n + 3, (d) 2n+n32^n + n^3.

Answer

(a) O(n)O(n), (b) O(n2)O(n^2), (c) O(log⁡n)O(\log n), (d) O(2n)O(2^n).

Solution

Keep the fastest-growing term and drop its constant. In (d) any exponential eventually beats any power: 2n>n32^n > n^3 for every n≥10n \ge 10.

Exercise 3: Count the steps

How many times does steps += 1 run in this code, as a formula in nn? What is its Big-O?

text
for i in range(n):
    for j in range(i):
        steps += 1
Answer

n(n−1)2\frac{n(n - 1)}{2} times, which is O(n2)O(n^2).

Solution

The inner loop runs 0,1,2,…,n−10, 1, 2, \ldots, n - 1 times:

0+1+2+⋯+(n−1)=n(n−1)2=n22−n2 → O(n2)0 + 1 + 2 + \cdots + (n - 1) = \frac{n(n - 1)}{2} = \frac{n^2}{2} - \frac{n}{2} \ \to\ O(n^2)
python · try it yourself
def count(n):
    steps = 0
    for i in range(n):
        for j in range(i):
            steps += 1
    return steps

print([count(n) for n in [4, 10, 100]], [n * (n - 1) // 2 for n in [4, 10, 100]])
[6, 45, 4950] [6, 45, 4950]

Exercise 4: When does exponential win?

Algorithm A takes 1000⋅n21000 \cdot n^2 steps and algorithm B takes 2n2^n steps. Find the smallest n≥1n \ge 1 for which B is slower than A from then on. Use a short Python search.

Answer

n=19n = 19 (B is slower for every n≥19n \ge 19).

Solution

Compare the two around the crossover:

n=18:218=262 1441000⋅182=324 000B still fastern=19:219=524 2881000⋅192=361 000B slower\begin{aligned} n = 18{:} &\quad 2^{18} = 262\,144 && 1000 \cdot 18^2 = 324\,000 && \text{B still faster} \\ n = 19{:} &\quad 2^{19} = 524\,288 && 1000 \cdot 19^2 = 361\,000 && \text{B slower} \end{aligned}

From there 2n2^n doubles with every step while 1000⋅n21000 \cdot n^2 grows by less than 11%, so B stays slower. The search finds the last nn where B is still at most as slow as A, and adds 1:

python · try it yourself
last_fast = max(n for n in range(1, 100) if 2**n <= 1000 * n**2)
n = last_fast + 1
print(n, 2**n, 1000 * n**2)
19 524288 361000

Despite its 1000× head start, the quadratic algorithm wins from n=19n = 19 on.

Test yourself

What is 4n3+50n2+94n^3 + 50n^2 + 9 in Big-O notation?

  1. O(4n3)O(4n^3)
  2. O(n3)O(n^3)
  3. O(n5)O(n^5)
  4. O(n2)O(n^2)

Binary search on 1 million sorted items needs about how many steps?

  1. 20
  2. 1 000
  3. 500 000
  4. 1 000 000

Doubling the input of an O(n2)O(n^2) algorithm multiplies its running time by about:

  1. 2
  2. 4
  3. log⁡2\log 2
  4. nn