1.2.38 · D1Introduction to Programming (Python)

Foundations — Classic recursion — factorial, Fibonacci, binary search

2,852 words13 min readBack to topic

This page assumes you know nothing. Every symbol on the parent page is unpacked below, in an order where each idea leans only on the ones before it.


0. The picture that everything rests on

Before any symbol, look at the shape of what recursion does.

Figure — Classic recursion — factorial, Fibonacci, binary search

1. A variable and the symbol =

The picture: a box with a name-tag n and the number 3 sitting inside.

Why the topic needs it: every recursive function has an input box (usually named n, lo, hi, or target). To follow a trace you must know a name just points at whatever is currently in its box.


2. The symbol n and what "smaller" means

The picture: the height of the tall box in §0. A "smaller subproblem" simply means a shorter box — , or , or a shorter slice of a list.

Why the topic needs it: recursion only terminates if each call is strictly smaller. If the box never shrinks, you never reach the tiny base box, and the pile grows forever.


3. The four arithmetic symbols +, -, * (×), /

Every formula on the parent page is built from just four operations. Here they are, from zero.

Why the topic needs it: n - 1 (subtract) is how factorial and binary search make the problem smaller; F_{n-1} + F_{n-2} (add) is the whole Fibonacci rule — glue two smaller answers together.

The parent writes factorial as a chain of multiplications:

Why the topic needs it: factorial is a product, and its base case is the empty product. Without this idea the base case looks arbitrary.


4. The factorial symbol !

The picture: a staircase of blocks multiplied together.

Figure — Classic recursion — factorial, Fibonacci, binary search

5. Subscripts and the Fibonacci symbols

The picture: a row of boxes labelled , each holding one Fibonacci number.

Why the topic needs it: because the rule reaches back two boxes, you need two known starting boxes () — that is exactly the "two base cases" the parent stresses.


6. Lists, indices, and the [ ] notation

Figure — Classic recursion — factorial, Fibonacci, binary search

7. Floor division // — why we need it beside /

Why the topic needs it: binary search always jumps to the middle slot, and slots are whole numbers — so it uses //, not /.


8. Comparisons: ==, <, >

The picture: a balance scale. arr[mid] < target tips left → the value is too small → the answer must be further right in a sorted list.

Why the topic needs it: every base case is a comparison — if n == 0, if lo > hi, if arr[mid] == target. Each one is a question that decides "stop, or recurse smaller."


9. Functions and the words def, return, "call"

The picture: a vending machine. You put inputs in (3), press go (call), and one value drops out (return). A recursive call is the machine reaching over and pressing its own button with a smaller input.

Why the topic needs it: recursion literally means "a function that calls itself." Without return, the smaller answer never travels back up the chain in §0.


10. The call stack — where "pile up then unwind" lives

Figure — Classic recursion — factorial, Fibonacci, binary search

Why the topic needs it: it explains why the multiplications in factorial wait, then combine bottom-up — the stack is the topic's hidden memory.


11. Growth symbols: , , and

The picture: is a gently rising curve (halving is fast); is a rocket (repeated multiplication). Same axis, wildly different climbs.

Why the topic needs it: these three symbols are how the parent's summary table compares factorial (), binary search (), and naive Fibonacci ().


12. Recurrence notation

Why the topic needs it: unrolling these recurrences is how the parent derives and . is recursion describing its own cost recursively — fitting.


Prerequisite map

The diagram below is a flowchart: each rounded label is one idea from this page, and an arrow means "you need the idea at the tail before the idea at the head." Read arrows as "feeds into." Follow any path from top to bottom and you are walking the safe learning order: basic symbols on the left/top, the three recursion problems in the middle, the cost language at the bottom.

Variable and assign =

Function def call return

Input n and smaller

Recursion big idea

Add subtract multiply divide

Factorial n!

Fibonacci

Binary search

Floor division //

Subscript Fn

List index lo hi mid

Comparisons == < >

Base cases

Call stack frames

Theta log phi and T of n

Time cost table


Equipment checklist

Test yourself — cover the right side and answer each.

What does n = n - 1 actually do?
Reads the box n, subtracts 1, and stores the result back into n (assignment, not equality).
What is the difference between = and ==?
= stores a value; == asks whether two values are equal (True/False).
What do + and - mean on a number line?
+ steps right (add amounts together); - steps left (take away).
Why does Fibonacci use +?
Each term is the sum of the two before it: .
What does mean and why does binary search use it?
Half of ; each step throws away half the list.
Why does ?
It is the empty product — multiplying nothing leaves 1, the value that changes nothing.
What does the subscript in mean?
The position in the sequence (the -th Fibonacci number), not times .
In arr[mid], is mid a value or a position?
A position (index); arr[mid] fetches the value living at that slot.
Why use // instead of / for mid?
Indices must be whole numbers; // rounds down so you land on a real slot, whereas / could give 2.5.
What is a stack frame?
A box holding one call's variables and its return spot; frames pile up then pop.
What causes RecursionError?
Calls never reach a base case, so frames pile up forever until memory runs out.
What does count?
How many times you can halve before reaching 1.
What does express?
The shape of how running time grows with input size, ignoring constants.
What does the recurrence describe?
Doing constant work then solving a half-size problem — the pattern behind .

Connections