The general consensus here is to throw some computing power at this problem, so I've written a program that creates permutations of all buttons an avaluates tham in calculator style. (All buttons except the equals sign, that is, because that is supposed to be pressed at the end.) Intermediate states of the permutations, i.e. those permutations that are shorter than 14 characters, are considered as well.
In order to cut the number of possibilities short, the following restrictions apply:
- There can't be zeros at the beginning of a number. Because this restrictions is applied when recursing, that also rules out any expressions with the number 0 in them, but that's find: Addition and subtraction of zero do nothing; multiplication results in zero and effectively resets the calculator and division by zero isn't allowed.
- There are no operators at the beginning or at the end of an expression. The only case where this matters is when there is a minus sign at the beginning. We are looking for positive interegs as results and there is no way to et a negative number except by subtraction, which means that we cannot get a positive number from a negative intermediate result by multiplication with or division by another negative number. Because all permutations are explored the expression −x + y can be ignored, because the equivalent y − x will be considered.
- Divisions are only done when the numerator is evenly divisible by the denominator. That means the expression 143 / 2×8 isn't evaluated, because the intermediate result 143 / 2 isn't an integer. This doesn't matter, because the expression 143×8 / 2 will also be generated and evaluates correctly to 572.
With this program, the first numbers that are unaccounted for are:
2652250, 3855830, 3866656, 3866698, 3919306, 3995545, 3997291
The program {1} is written in C and takes a bit less than half an hour on an older machine. On one of our newer machines here at work, it takes about 10 minutes to run. Parallelising it with OpenMP {2} will reduce the execution time significantly, it is about 1:20 minutes on the new machine with 16 threads.
There is a similar problem on PSE. It allows each of the decimal digits at most once and up to four uses of the operators (+, −, ×, /), but these operators may be repeated.
I've modified my program {3} to allow any operator, but the lowest gap from the most upvoted answer, 8480902, found without considering division, is only in the 12th or 17th place of the numbers I found with that program, depending on whether I allow division or not. My first gaps are:
6799999, 7175713, 7327237, 8189263, 8248438, 8296789, 8299073
That means the my program may still have errors and that these errors may also be present in the original program for the present problem, because the (possibly) erroneous program is a modification of it. Perhaps the restrictions I explained above are not sound.
On the other hand, that question allows parentheses, which are ruled out by the calculator rules here. So expressions like
(123 + 4) × (89 - 7)
are allowed, but they cannot be used on the calculator, because there is no means to store the second intermediate result. That difference may explain the different output between my program and the given answer.
I share the code for my programs via pastebin in order not to make this answer too long.
{1} https://pastebin.com/5DwcxLES — this question, sequential
{2} https://pastebin.com/nby6cEWS — this question, parallelised
{3} https://pastebin.com/KKYckiGi — other question, parallelised
1+2=x3-
; older/simpler calculators will display 3 when=
is pressed, then 9 when-
is pressed. Does this calculator have such behaviour, or should we assume that you have to enter a whole expression and then=
before it displays anything? $\endgroup$