Programming and Puzzles in Math

One way I relieve stress with my students is with puzzles. We are currently preparing for our school wide exhibition "Festival del Sol". I'll be sure to follow up with pictures when it happens in a couple of weeks, it is amazing. Even though our students have done two exhibitions (the challenges of engineering project, and a Edgar Allen Poe play) there is still a high possibility of getting stressed out as the deadline approaches. I have a weekly puzzle on the board so we can take a moment to have some mathematical play.

I always choose puzzles that I do not know the answer to or that I have forgotten so I can enjoy working it out with the students as well. Often it inspires me to write a Python program to solve it. My students think I am cheating but I always tell them that someone has to program the computer, it can't just solve it for me.

This week's puzzle was:

Find the five digit number in which the first digit is two more than the second, the second digit is two more than the third, the fourth digit is two less than the third, and the last digit is two more than the fourth. The sum of the third, fourth, and fifth digits equals the first. The sum of all the digits is 19. What is the five-digit number?

Here is my solution, it is definitely one of the most nested loops I remember writing. Although I wasn't writing it for efficiency, I still reduced the loops to 9 because that is the biggest it could be for a single digit. I never cease to be amazed at how quickly the program can solve the problem.

for first in range(1,9):    
     for second in range(1,9):         
              for third in range(1,9):             
                     for fourth in range(1,9):                
                           for fifth in range(1,9):                     
                                if first+second+third+fourth+fifth == 19:                         
                                        if (first == second +2) & (second == third+2) & (fourth == third-2) &((third+fourth+fifth)== first):                                         
                                             print ("The answer is first: %i, second: %i, third: %i, fourth: %i, fifth: %i" % (first, second, third, fourth, fifth))   

Now for a real challenge. One of my favorite puzzles came from Henry Dudeney almost 90 years ago.

A college student sent a letter home to his parents saying, "Hey Mom and Dad, I need some more cash because college is expensive." The parents wrote him back saying, "We have spent all of this money to educate you and yet you ask us for money like this? You didn't even say how much you need." The student sends a letter back saying:


Where each letter represents a unique digit from 0-9. How much money was the student asking for? Now you can of course Google the answer but I hope you will give yourself the time to either solve it by hand or program it.

Subscribe to BrokenAirplane for great tutorials and educational news.