Implements problem 4 of chapter one