Sub topic 2
How Functions Grow: Comparing n, n Squared, and 2 to the n
This chapter assumes you can write loops and recursive functions, read basic algebra with exponents, and use lists. Logarithms help but are not required. The previous chapter counted operations as a function of input size n. Here you compare how those counts grow.
Why growth matters more than a single measurement
Suppose algorithm A takes 100n steps and algorithm B takes n² steps. For n = 10, A takes 1,000 steps and B takes 100, so B looks better. For n = 1,000, A takes 100,000 steps and B takes 1,000,000. The curves cross at n = 100, and B is slower for every larger input.
Growth rate describes what happens to the cost as n gets large. A function that grows faster eventually overtakes any function that grows slower, no matter how large the constant factor on the slower one.
Comparing the main families
The table shows step counts for five functions. Read each row to see how quickly the numbers separate.
| n | log₂ n | n | n² | 2ⁿ |
|---|---|---|---|---|
| 10 | 3 | 10 | 100 | 1,024 |
| 20 | 4 | 20 | 400 | 1,048,576 |
| 40 | 5 | 40 | 1,600 | about 1.1 × 10¹² |
| 100 | 7 | 100 | 10,000 | about 1.3 × 10³⁰ |
Three patterns appear:
- Doubling n from 20 to 40 doubles the linear cost and quadruples the quadratic cost.
- Adding 1 to n doubles the exponential cost. Going from n = 20 to n = 40 multiplies it by 2²⁰, about a million.
- The logarithm barely moves. Doubling n adds only 1 to log₂ n.
Seeing the crossover in code
This program counts steps for the two algorithms above and finds where B becomes slower than A.
def cost_a(n):
return 100 * n
def cost_b(n):
return n * n
crossover = next(n for n in range(1, 1000) if cost_b(n) > cost_a(n))
print("B is slower than A from n =", crossover)
for n in (10, 100, 1000):
print(n, cost_a(n), cost_b(n))It prints a crossover at n = 101. At n = 100 the two costs are equal, and from 101 on the quadratic one is larger.
A picture of the ordering
The diagram shows the ordering that holds once n is large enough. An arrow points from a slower-growing function to a faster-growing one.
Polynomial versus exponential
Functions like n, n², and n³ are polynomial: n is raised to a fixed power. A function like 2ⁿ is exponential: n appears in the exponent. Every exponential with base greater than 1 eventually beats every polynomial. For example, 2ⁿ passes n¹⁰ once n is a little over 58.
This split matters in practice. If you double the speed of your computer, a polynomial algorithm can handle a larger input by a constant factor. An exponential algorithm gains only a few extra items of input. With 2ⁿ steps, a machine twice as fast handles n + 1 instead of n.
Common mistakes
- Treating a larger constant as a larger growth rate. 1000n still grows more slowly than n².
- Confusing n² with 2ⁿ. At n = 3 they are 9 and 8, which are close. At n = 30 they are 900 and over a billion.
- Expecting a fast machine to rescue an exponential algorithm. The extra speed buys about one more unit of n per doubling.
Exercise
Algorithm X takes 50n² steps and algorithm Y takes 2ⁿ steps. At n = 10, which is cheaper? At n = 30, which is cheaper? Which one grows faster as n becomes large?
Show answer
At n = 10, X takes 50 × 100 = 5,000 steps and Y takes 1,024, so Y is cheaper. At n = 30, X takes 50 × 900 = 45,000 steps and Y takes 2³⁰ = 1,073,741,824, so X is cheaper. Y grows faster, because every exponential 2ⁿ eventually exceeds any polynomial, whatever its constant factor.