This is an algorithm trace task. No implementation is required.
Read the following algorithm for the recursive function called Curiosity that needs two integers as inputs and returns an integer value.
In the algorithm, MOD is an operator. It works with two integers to give the remainder when the first integer is divided by the second. This means that
- 17 MOD 7 is 3
- 72 MOD 15 is 12
(a) Write down the output when the function is called with
- Output ‘The answer is ’, Curiosity(120,42)
- Output ‘The answer is ’, Curiosity(221,153)
(b) The function is called with
- Output ‘The answer is ’, Curiosity(30,24)
Write down in full each line that is executed, but replace each variable with its actual value and indicate on the IF statement line whether the result of the test is true or false. [7 Marks]
(c) Describe the purpose of the algorithm. [2 Marks]
(d) Rewrite the recursive function as an iterative algorithm. [6 Marks]
IMAGE CREDITS: https://www.nasa.gov/