Problem 5 from 52 programming problems book