Algorithms and complexity · A level · OCR H446 2.3.1, AQA 7517 4.4.4.1, Eduqas A500QS 1.3 · about 25 min
Time and space efficiency as functions of the size of the problem; linear, polynomial, exponential and logarithmic functions; permutations and n!.
[1 mark]Why are algorithms compared by counting operations as a function of n rather than by timing them?
[1 mark]Which of these is an exponential function of n?
[1 mark]What is log₂ 256?
[1 mark]In how many different orders can a robot visit 5 distinct checkpoints?
[1 mark]Checking a list for repeats by storing every item seen in a set, instead of comparing every pair, makes which of these true?
Tick every answer that is true.
[1 mark]What does this print?
n = 1
for k in range(1, 6):
n = n * k
print(n, 2 ** 5, 5 ** 2)120 32 25
The loop works out 5! = 120, which is already bigger than 2⁵ = 32 and 5² = 25.
Print a table of how four functions grow. For each n in 4, 8, 16, 32 and 64, print one line in exactly this form:
n=8 log2=3 square=64 exponential=256
where log2 is log₂ n (a whole number here, because every n is a power of 2), square is n² and exponential is 2ⁿ. Work out log2 by counting how many times n can be halved (with // 2) before it reaches 1, not with the math module.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
n = 8
print("n=" + str(n))The hint students can ask for: Loop over the five values of n. For each one, copy n into another variable and keep halving the copy while it is bigger than 1, counting as you go. Build the line from the four numbers.
from bugbot import *
connect()
for n in [4, 8, 16, 32, 64]:
halves = 0
m = n
while m > 1:
m = m // 2
halves = halves + 1
print("n=" + str(n), "log2=" + str(halves), "square=" + str(n * n), "exponential=" + str(2 ** n))
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.