
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.
1 2 3 4 5 6 7 8 | 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:
SEND + MORE = MONEY
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.