Problem 4

Pending revision to improve efficiency.