C.W.K.
Stream
Lesson 02 of 05 · published

An Algorithm Is a Recipe With a Price Tag

~11 min · foundations, algorithms, cost

Level 0Curious Beginner
0 XP0/85 lessons0/19 achievements
0/100 XP to next level100 XP to go0% complete
"Correctness gets you in the door. Cost decides whether you're allowed to stay."

What Makes Something an Algorithm

An algorithm is a recipe: a finite list of well-defined steps that turns an input into an output. "Finite" matters — a recipe that never ends isn't a recipe, it's a stovetop fire. "Well-defined" matters — "add salt to taste" is fine for a human cook but useless to a machine, which needs "add 4 grams of salt." Every step has to be unambiguous and actually finishable.

That's the boring half. Here's the half that matters: the same correct output can be produced by recipes with wildly different price tags.

Two Recipes, Same Dish, Different Bill

Say you want the sum of every number from 1 to n. Recipe A: start at 0, add 1, add 2, add 3 … add n. For n = 1,000,000 that's a million additions. Recipe B: notice that the numbers pair up (1 + n, 2 + n−1, …) and the answer is just n * (n + 1) / 2 — three operations, no matter how big n gets. Both give the identical, correct answer. One does a million steps; one does three.

The nine-year-old Gauss supposedly spotted Recipe B in seconds while his classmates ground through Recipe A by hand. That's not about being a genius — it's about seeing that the obvious recipe and the cheap recipe are usually not the same recipe. Training that eye is what this quest is for.

Two algorithms can be equally correct and unequally affordable. Correctness is necessary; cost is what makes it usable at scale.

Why "At Scale" Is the Whole Story

For n = 10, who cares — both recipes finish before you blink. The cost difference only screams when the input grows. A program that's snappy on your laptop's 100 test rows and dies on production's 100 million rows wasn't wrong. It was expensive, and nobody measured the price until the bill came due. The next track, Complexity, gives you the tool to read that price tag before you ship.

Pippa's Confession

I used to declare a function "done" the moment the tests passed. Dad's question every single time: "Okay — and what happens when the input is a thousand times bigger?" Half the time I had no idea, because I'd never looked. Correctness made me feel finished; he taught me that finished is correctness plus a cost I can actually defend.

Code

A million steps vs three·python
# Same correct answer. Two very different price tags.

def sum_loop(n: int) -> int:
    """Recipe A: add them one at a time. n steps."""
    total = 0
    for i in range(1, n + 1):
        total += i
    return total

def sum_formula(n: int) -> int:
    """Recipe B: Gauss's pairing. 3 steps, forever."""
    return n * (n + 1) // 2

for n in (10, 1_000_000):
    assert sum_loop(n) == sum_formula(n)   # identical answers
    print(n, sum_formula(n))

# sum_loop(1_000_000) did a million additions.
# sum_formula(1_000_000) did one multiply, one add, one divide.
# Same output. The bill is not the same.

External links

Exercise

Write out a real algorithm you run every day — your morning routine, your commute, how you make coffee — as a numbered list of well-defined steps. Then circle the one step that takes the longest. If your input doubled (twice the dishes, twice the distance), which steps stay the same and which blow up?
Hint
Steps that touch every item (wash each dish) blow up with input size; steps that happen once (turn on the kettle) don't. That difference is the seed of complexity.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.