Generating function for exercise 5.1