time4dan's solution is fantastic.

*I don't know if you have already considered this*, but there may be multiple answers to the equation, and that you should not just compare the users answer to your set of generated answers, but also actually

*calculate* the users set of answers.

An example (ridiculously simplified to get across the point):

if your program generates something like this: '1 * 1 * 1 * 1 = 1' (the user see's 1_1_1_1=1),

With time4dan's solution, the '*' signs are stored in memory as a possible solution,

but there is also a possible solution of '1 / 1 / 1 / 1 = 1'.

What time4dan's solution does really well is that it make sure that there is AT LEAST one solution to the equation. You just need to expand it to take into account the other possible solutions.

One possible extension of time4dan's solution would be to just take a brute force approach, after the numbers and signs are generated. Test the equation for all the different combinations of signs, and hold the possible answers in a list. For example:

The random numbers are: 2, 6, 1, and 3

The random signs are: '+', '-', and '*'

This gives us the equation '2+6-1*3 = 21' (calculated as such "( (2+6) - 1) * 3" )

So the next thing would be to find all the possible solution to this equation using a brute force approach (because the are only '3' signs, this should be quick - this solution does not scale well when the number of signs are increased. ), and could be done like so

(psuedo code)

A= 2

B= 6

C= 9

D= 3

E= 21

if A+B+C+D=E { add ('+', '+', '+') to the list of solutions }

if A+B-C+D=E { add ('+', '-', '+') to the list of solutions }

if A+B+C-D=E { add ('+', '+', '-') to the list of solutions }

... etc

There are ~64 checks in total when there are only '3' signs, so it shouldn't be too slow.

A simpler solution would be to just take the users entered answers and calculate them directly, with the generated numbers.

**Edited by rude guss, 18 February 2012 - 07:20 AM.**