Year Problem

Solutions to the year problem

The puzzle is presented at: http://www.npr.org/2012/01/29/146034893/this-puzzle-is-the-pits

Next Week's Challenge from listener Ed Pegg Jr.: Write the digits from 1 to 9 in a line. If you put times signs after the 2 and 4, a plus sign after the 5, and a minus sign after the 7, you have 12 x 34 x 5 + 67 - 89, which equals 2018. That's six years off from our current year 2012. This example uses four arithmetic symbols. The object is to use just three of the following arithmetic operations: addition, subtraction, multiplication and division, in a line from 1 to 9 to get 2012 exactly. The operations should be performed in order from left to right. There are no tricks to this puzzle. Can you do it?

We wrote a program to find the answer, in both Java and Python versions.  The Java version runs from the Web page on real computers (but not on cellphones, which don't support Java).  As can be seen from the Java version output above, there is only one solution for 2012.  So as not to make this too easy for next year, we hard-coded 2012 into the program, but a little tweaking shows that 2013 too has exactly one solution and there is a year in recent decades with two solutions.

Java source code is here.

Python source code is here.

A listing of many Java resources is at this link.