Solution by Duncan C. White
