Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 2 of 2.
Write any two of these:
Recursion is a process where a function or procedure is defined in terms of itself (it calls itself) [1]
It needs a base case (a way to return without making a recursive call) [1]
Other points that also score: there must also be a general case where the recursive call happens.
Calls go down until Number is 0 or 1 (the base case), then answers are added back up (unwinding).
| Call number | Function call | Number | Result | Return value |
|---|---|---|---|---|
| 1 | Fib(5) | 5 | Fib(4) + Fib(3) | |
| 2 | Fib(4) | 4 | Fib(3) + Fib(2) | |
| 3 | Fib(3) | 3 | Fib(2) + Fib(1) | |
| 4 | Fib(2) | 2 | Fib(1) + Fib(0) | |
| 5 | Fib(1) | 1 | 1 | 1 |
| 6 | Fib(0) | 0 | 0 | 0 |
| (4) | Fib(2) | 2 | 1 + 0 | 1 |
| (3) | Fib(3) | 3 | 1 + 1 | 2 |
| (2) | Fib(4) | 4 | 2 + 1 | 3 |
| (1) | Fib(5) | 5 | 3 + 2 | 5 |
Call number column correct [1]
Function call and Number columns correct [1]
Result column correct going down to the base case (rows 1 to 6) [1]
Result column correct coming back up (rows 7 to 10) [1]
Return value column correct, ending with 5 [1]
▶ Whole paper
Video solution of the whole paper · by qiphyedu. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.