solution for Problem 31