Back to courses

MATH40006 Practice Paper 6

English review edition prepared on 4 October 2026 from a preserved source copy. It is a later presentation, not the historical study interface. Source checks and difficulty judgements describe the original material author's own process; they do not indicate Imperial College London endorsement.

Source SHA-256: ba9cfe1e639355d95613316d8be2ecb9cd87bec26df7f4a38bc73bc7b81d7a53
Source date: 2026-08-06

Notes — cell 1

# MATH40006 Revised Historical-Coverage Mock 6

**Time allowed:** 60 minutes 
**Total marks:** 50 
**Questions:** 3 (20 + 20 + 10) 
**Role:** Mixed migration pressure: codebooks, instrumented algorithms and representation transforms

Answer all questions in this Jupyter notebook. Show testing code and outputs. Unless the question explicitly requests an in-place operation, functions should return a value without changing their inputs.

Code — cell 2

import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
import pandas as pd
from time import perf_counter
from copy import copy
import pickle
import ast
import math
sp.init_printing()

words=['amber','bridge','circle','delta','ember','forest','globe','harbour','island','jungle','lantern','meadow']
plaintext='amber forest island delta meadow'

def binary_digits(n): return [int(c) for c in bin(n)[2:]]
def from_binary_digits(bits): return int(''.join(map(str,bits)),2)

Notes — cell 3

## Question 1 (20 marks)

A list of distinct words is supplied.

(a) With a fixed NumPy seed, make a separately copied shuffled list `code_words`; demonstrate that `words` is unchanged. **[3]**

(b) Build a sorted list of `(plain,code)` pairs, a dictionary and a Series. **[3]**

(c) Write iterative binary search `pair_partner(pairs,key)` returning `'failed'` for a missing key. **[5]**

(d) Encode a supplied sentence with binary search and separately with the dictionary; verify equality. **[3]**

(e) Build the reverse mapping and decode the result. **[3]**

(f) Explain briefly why `code_words=words` would break the design. **[3]**

Code — cell 4

# YOUR CODE HERE
raise NotImplementedError()

Notes — cell 5

## Question 2 (20 marks)

(a) Write an instrumented discrete Newton integer-square-root function returning `(root,iteration_count)`, with a bit-length starting value above sqrt(n). **[6]**

(b) Test defining inequalities on small, square-adjacent and hundred-digit inputs. **[3]**

(c) Record iteration counts for n=2**100,2**200,...,2**2000 and compare them with log2(log2(n)); comment cautiously on Theta(log log n). **[3]**

(d) Build a nested dictionary containing runtime and iteration count for each input. **[4]**

(e) Export the dictionary with pickle, convert it to a DataFrame, export CSV, and confirm both files. **[4]**

Code — cell 6

# YOUR CODE HERE
raise NotImplementedError()

Notes — cell 7

## Question 3 (10 marks)

The helpers convert positive integers to and from binary digit lists.

(a) Verify they are inverse for n=1,...,12. **[2]**

(b) Write `run_lengths(bits)`. Example: `[1,1,1,0,0,1] -> [3,2,1]`. **[4]**

(c) Write `run_heads_value(n)`: retain only the first bit of each run and convert back to an integer. **[3]**

(d) Test for n=1,...,20 and state the running-time order in terms of n. **[1]**

Code — cell 8

# YOUR CODE HERE
raise NotImplementedError()