Solution to problem 10

Two ways of solving problem 10.( one is significantly better than the
other)