1 Algorithm X is given below:
STEP 1: \(\quad\) Let \(A = 0\)
STEP 2: Input the value of \(B \quad [ B\) must be a positive integer]
STEP 3: \(\quad C = \operatorname { INT } ( B \div 3 )\)
STEP 4: \(D = B + 3 C\)
STEP 5: \(\quad\) Replace \(A\) with \(( D - A )\)
STEP 6: Decrease \(B\) by 1
STEP 7: If \(B > 0\) go to STEP 3
STEP 8: Display the value of \(\operatorname { ABS } ( A )\) and STOP
- Trace through algorithm X when the input is \(B = 5\).
Only record changes to the values of the variables, \(A , B , C\) and \(D\), using a new column of the table in the Printed Answer Booklet each time one of these variables changes.
- Explain carefully how you know that algorithm X is finite for all positive integer values of \(B\).
- Explain what it means to say that an algorithm is deterministic.
The run-time of algorithm X is \(k \times\) (number of times that STEP 6 is used), for some positive constant \(k\).
- Explain, with reference to your answer to part (b), why the order of algorithm X is \(\mathrm { O } ( n )\), where \(n\) is the input value of \(B\).
For each input \(B\), algorithm Y achieves the same output as algorithm X , but the run-time of algorithm Y is \(m \times\) (the number of digits in \(B\) ), where \(m\) is a positive constant.
- Show that algorithm Y is more efficient than algorithm X .