problem 22
more straightforward code