more general solution to problem 13
solution to problem 14

also optimised problem 12