# Reverse Math?

• 09-03-2013, 06:44 PM
LG87
Reverse Math?
Hi fellow code lovers.

Guess I should first tell you my case:
I'm creating a game that is supposed to give out, lets say 4 numbers, and four operators (+ - * /).
And the numbers (or a subset) and/or the operators should be able to produce a number, e.g: 24.
Anyone have a idea how I can let's say: Random generate 4 numbers, then check if they will be able to provide 24 given
the option to place the numbers and the operators at whichever place you want.

I've been struggling to find a way to validate the numbers produced.
If anyone got a link, an example solution, or something similar, it would be extremely appreciated.

Kind regards, LG.
• 09-03-2013, 06:57 PM
KevinWorkman
Re: Reverse Math?
One of the hardest parts of programming is taking something that you "just do in your head" and figuring out exactly what you're actually doing, step by step. That's the problem you're faced with, not actually coding it up yet.

Pretend you have a really dumb friend who doesn't really know how to figure out whether 4 numbers can combine to form 24. Write out a series of steps (in your own words, not pseudocode or code) that your friend could follow to accomplish the goal. Remember how dumb your friend is, so if one of the steps can be broken down into simpler steps, do it.

When you have those steps written out, you'll have an algorithm that you can start thinking about implementing.
• 09-03-2013, 07:47 PM
JosAH
Re: Reverse Math?
Quote:

Originally Posted by KevinWorkman
Pretend you have a really dumb friend [ ... ]

Don't gossip about me, I'm not dumb, I may look a bit funny but that's all ;-) I solved a similar problem once: it was about a silly game we played in the pub; it involved a sharp mind, intelligence, bright math skills and a lot of beer. After solving that silly game it was completely ruined ;-)

Think of expressions in postfix form (operators follow their operands). Postfix expressions are easy to generate and its easy to check whether or not a potential expression is a valid expression. If the result matches the target you have a hit. Beware that you have to evaluate a lot of expressions.

kind regards,

Jos
• 09-03-2013, 10:07 PM
kimi
Re: Reverse Math?
random numbers and random operators? That seems a kinda hard problem... you should probably try with backtracking or memoization...
• 09-03-2013, 10:08 PM
gimbal2
Re: Reverse Math?
can't quote right now, so @JosAH: you're a cool dude, good sense of humor. But sometimes I just can't make heads or tails of what you are saying man! :)

I fully accept that it is very likely me that has the limitations.
• 09-04-2013, 08:53 AM
JosAH
Re: Reverse Math?
@gimbal2: same here, the forum software is (partly broken again); about that problem: there are four operators and four operands; give them index values 1 ... 8, where (arbitrarily chosen) the numbers 1 ... 4 are the operands (numbers) and 5 == +, 6 == -, 7 == * and 8 == /. A postfix expression is a permutation P(8, n) of the numbers 1 ... 8. Not every posfix expression makes sense, e.g. 5, 6, 7, 8 is a nonsense expression. That is easy to check. Simply generate all the 8! permutations and evaluate the resulting expressions ... clear now amigo?

kind regards,

Jos
• 09-04-2013, 09:00 AM
gimbal2
Re: Reverse Math?
yep!