How can 5×4 become 5 + 5×3 — and why does that one rewrite of the same problem make the loop disappear entirely?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Here is the while-loop version of multiplication from the video:
def mult_iter(a, b): result = 0 while b > 0: result += a b -= 1 return result
Run mult_iter(7, 3) in your head. Every time the loop body finishes, result is holding a new value. Assign to trace a list of those values, in the order they appear — one entry per completed pass, and do not include the starting 0.
Iterative algorithm: loop plus state variables
State that an iterative algorithm is a loop with variables holding the state of the computation, and list the four questions: what changes each pass, what counts, when it stops, what is returned.
mult and mult_iter built from addition alone
Write both functions, a for loop over range(b) accumulating into total and a while b > 0 loop adding into result as b -= 1 counts down, with both giving 20.
The pattern 5*4 = 5 + 5*3
Write the substitution chain from 5*4 down to 5*1, extracting one 5 per line with parentheses around the piece being replaced, and mark 5*1 = 5 as the fact known outright.