How can you read Theta straight off the loops: when do costs add, when do they multiply, and where do the six complexity classes rank?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The video's law-of-addition program runs one loop over range(n) and then, once that loop has finished, a second loop over range(n * n); each body only prints a letter. Define series_passes(n) that runs those same two loops, bumping a counter where each print was, and returns how many times a loop body ran in total.
From operation count to Theta in fact_iter
List the five steps of one while iteration in fact_iter, write the total , and reduce it to by dropping both the additive and the multiplicative constant.
Law of addition in series, multiplication when nested
Write both laws as formulas, then work the two loops in series to and the nested loops, with the inner range(n//2) still , to .
Naming the input parameter first: f(x) and f(L)
Work f(x) to by summing the inner range(i,x) runs , then state the list-squaring f(L) as , counting only loops that depend on the input.
Ranking the classes from constant to exponential
List the six classes from down to with their names, most efficient first, and copy the growth-table values for linear, polynomial and exponential at , and .