produce rather ugly solution to problem 40