implements problem 6 of chapter one