Math for Programming / Part 2: Math inside the computer

Growth: why some programs stay fast

Linear, quadratic, exponential, and logarithmic functions are not just shapes on a graph. They predict whether a program finishes in a blink or never finishes at all.

Lesson 12 of 13About 12 minutesBuilds on: linear and exponential functions

Counting steps instead of seconds

Two programs can give the same answer while one takes a second and the other takes a week. Computer scientists compare methods, called algorithms, by counting how many steps they need as the input grows. The question is not “how fast is it today?” but “what happens when there are ten times as many items?”

Searching a list two ways

To find a name in an unsorted list, you may have to check every entry: 1,000 names can take 1,000 checks. That is linear growth: double the list, double the work.

If the list is sorted, you can do far better. Open to the middle; if your name comes earlier, throw away the second half, otherwise throw away the first half. Each check cuts the list in half. This is binary search. Because 2¹⁰ = 1,024, ten halvings shrink 1,000 names to one. A million names take only about 20 checks, because 2²⁰ = 1,048,576.

Figure

Think of a whole number from 1 to 100. The computer will find it with binary search.

Is it 50?

Range 1–100 · guesses so far: 0 · never more than 7 needed, because 2⁷ = 128 ≥ 100

Logarithms answer “how many halvings?”

The number of times you can halve n before reaching 1 is the base-2 logarithm of n, written log₂ n. log₂ 8 = 3, because 8 → 4 → 2 → 1. Logarithms grow very slowly, which is why sorted data and binary search are so powerful. Logarithms are the inverse of exponents: if 2ᵏ = n, then k = log₂ n.

When work explodes

Some tasks compare every item with every other item. Checking every pair of 100 game objects for collisions takes 100 × 99 ÷ 2 = 4,950 checks. With 1,000 objects it is 499,500. Ten times the objects, about a hundred times the work. That is quadratic growth, and it is why games divide the world into grid cells and only test nearby objects.

Worse still is trying every arrangement. Nine digits can be ordered in 9! = 9 × 8 × 7 × … × 1 = 362,880 ways. Twenty items have about 2.4 × 10¹⁸ orders, far too many for any computer to list. Clever searches avoid listing them all. Backtracking builds an arrangement one item at a time and abandons it the moment it breaks a rule, skipping every arrangement that starts the same way.

NameExamplen = 10n = 1,000n = 1,000,000
Constant, O(1)look up item #5111
Logarithmic, O(log n)binary search41020
Linear, O(n)check every item101,0001,000,000
Quadratic, O(n²)compare every pair1001,000,00010¹²
Exponential, O(2ⁿ)try every on/off combination1,024≈ 10³⁰¹unthinkable

The “O” notation, called Big O, keeps only the part of the step count that matters most as n grows. It ignores small details so you can compare the shape of the growth.

GDScript
# Linear search: up to n checks
func find(items: Array, target) -> int:
    for i in items.size():
        if items[i] == target:
            return i
    return -1
# Godot arrays also have items.bsearch(target) for sorted arrays.
Common mistake

Testing only with tiny inputs. A quadratic method feels instant with 20 items and freezes with 20,000. Ask how the step count grows before trusting a method with real data.

Where this comes from in school math

Linear, quadratic, and exponential functions are the main families in Algebra I and II, and logarithms arrive as the inverse of exponential functions. The pairs formula n(n − 1)/2 is the handshake problem from counting units. Big O is a way of comparing these function families at large inputs.

Where you will see it

  • Search engines and databases: sorted indexes make lookups logarithmic.
  • Games: spatial grids avoid checking every pair of objects.
  • Puzzles: smart searches prune arrangements instead of trying them all.
  • Security: passwords are strong because guessing every combination grows exponentially.

Exercises

About how many binary-search steps does a sorted list of 4,000 items need?

2¹² = 4,096, so about 12.

How many handshakes happen when 30 people each shake hands once with everyone else?

30 × 29 ÷ 2 = 435.

A method takes 1 second for 1,000 items and is quadratic. Roughly how long for 10,000 items?

Ten times the items means 10² = 100 times the work, about 100 seconds.