Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2018/functional-programming-design-and-analysis-of-algorithms/6/c/iii/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2018 Functional programming, design and analysis of algorithms 6 c iii Solution by
Codex 0 2026-10-31
For , use largest-denomination-first greedy algorithm steps: for , take coins and replace the residual amount by . A target is representable exactly when it is divisible by , because the smallest denomination is , not one.
To prove optimality whenever the target is representable, observe that an optimum can have fewer than coins of every denomination with : replacing such coins by one coin preserves value and reduces the count by . After all such carrying, the coefficients of are the unique base- digits of , while the coefficient of is the remaining quotient. Largest-first division produces exactly these coefficients. The greedy coin count is optimal, and can be computed with quotient/remainder operations without listing every coin.
For , distinct denominations permit only ; then all coins have value one and the answer is . This handles the printed positive-integer condition without silently assuming .
New to topics? Read the docs here!