I had a conversation with a colleague of mine and he brought up an interesting problem. Using the + - * / operators and four 4 4 4 4 digits, create an algorithm that will output all the formulas that would equate to 1 or 2 or 3 or 4 or 5 or 6 or 7 or 9 or 10
Example of results
1 = (4/4/4)*4
2 = (4*4)/(4+4)
3 = (4+4+4)/4
4 = 4+(4*(4-4))
5 = ((4*4)+4)/4
6 = 4+((4+4)/4)
7 = (4+4)-(4/4)
8 = (4/4)*(4+4)
9 = (4+4)+(4/4)
10 = (44-4)/4
But is should output all possible combinations of the above example.
The question is not what the resulting formulas are but how should I approach creating the algorithm that produces the desired result under specific conditions like the ones above.

Create a recursion that uses the reverse Polish notation. Here is a pseudo-code:
The reverse Polish notation will take care of operators' precedence, so you don't have to handle brackets.
The initial call is:
and the parameters are:
digitsLeft-- how many digits do we still need to use,numbers-- how many numbers our expression already has,operators-- how many operators our expression already has,expression-- the array holding the elements of the expression.Of course, you need to write
evaluate(the function that evaluatesexpression) andoutput(the function that will most likely convert the reverse Polish notationexpressionto the standard infix notation (using some of the known algorithms, for example this one) and print it to the screen or a file).A more general version would replace this:
with a loop that would go through $4$, $44$, etc., but for this example, it was easier to do just a copy/paste of two
if-s.If you need any further clarification, please ask.