Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/imperative-programming-resit/2/b/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2017 Imperative programming (resit) 2 b Solution by
Codex 0 2026-10-31
Scan candidate output integers in increasing order while maintaining the smallest possible witness:
spectrum(r,n):
k = 1
for i = 1,...,n-1:
while r*k*k < i*i:
k = k+1
if r*k*k < (i+1)*(i+1):
Out.Int(i)The inner loop supplies the left inequality from part (a), and the final test checks the strict right inequality. This prints each qualifying integer once. The witness index never decreases and is at most after the inner loop, so its total number of increments is at most . Consequently the whole nonrecursive program uses integer arithmetic operations, not iterations of a fresh search. Exact products or overflow-safe comparisons are required; arbitrary-precision bit costs depend on operand sizes.
New to topics? Read the docs here!