3 ms·
Here is a script in Python that exhaustively calculates all values, then spits out the formulas for the 1900-2100 range: http://pastie.org/777044 http://pastie
by jcl 17y ago
Here is a script in Python that exhaustively calculates all values, then spits out the formulas for the 1900-2100 range: http://pastie.org/777044 http://pastie.org/777044
The code is about 30 lines and runs in under seven seconds on my machine.
It works by splitting the list of numbers into all possible sets of two sublists, recursively calculating the possible values of expressions for each sublist, then merging the expression values together using each possible operator.
I think this approach runs faster than the Java/Lisp solutions because it can collapse equal-valued subtrees together, which helps particularly when you have a subtree that consists only of additions, multiplications, or divisions.
(Edit: It also caches the value set of each subtree, which probably helps even more.)