๐ŸŒ€ Recursive Fibonacci

Math Function โ†’ Function Calls โ†’ Recursion โ†’ Python

EXA-18 ยท RECURSION

A Function That Calls Itself

Understand Fibonacci as a mathematical function first. Then watch the recursive calls before writing Python.

BASE CASE

Where recursion stops

F(1) = 1
F(2) = 1

These values are already known, so the function does not call itself again.

RECURSIVE RULE

Build a new value from two earlier calls

F(n) = F(nโˆ’1) + F(nโˆ’2)

For n > 2, solving one function means asking the same function to solve two smaller problems.

EXPAND THE MATH
F(3)=F(2)+F(1)
=1+1=2
F(4)=F(3)+F(2)
=2+1=3
F(5)=F(4)+F(3)
=3+2=5
F(6)=F(5)+F(4)
=5+3=8
INTERACTIVE CALL TREE

Watch F(5) call itself

F(5)
โ†™     โ†˜
F(4)
F(3)
recursive calls continue until F(1) / F(2)
F(3)
F(2)=1
F(2)=1
F(1)=1
F(2)=1
F(1)=1
CURRENT STEP
Ready

Press Play or Step.

CALL / RETURN TRACE
๐Ÿ’ป Recursive Fibonacci Lab

Translate the math function into Python.

PYTHON CONSOLE
Ready.