This is probably the final version before the hand-in. It is not perfect, though, as the algorithm in task b is inaccurate for small values of n.