Results 1 to 7 of 7
Thread: Reverse Math?
 09032013, 06:44 PM #1Member
 Join Date
 Jun 2011
 Posts
 8
 Rep Power
 0
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.
 09032013, 06:57 PM #2
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.How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 09032013, 07:47 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
Re: Reverse Math?
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 09032013, 10:07 PM #4Member
 Join Date
 Aug 2013
 Posts
 11
 Rep Power
 0
Re: Reverse Math?
random numbers and random operators? That seems a kinda hard problem... you should probably try with backtracking or memoization...
 09032013, 10:08 PM #5Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,114
 Rep Power
 9
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."Syntactic sugar causes cancer of the semicolon."  Alan Perlis
 09042013, 08:53 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 09042013, 09:00 AM #7Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,114
 Rep Power
 9
Similar Threads

Reverse a String
By MehwishS in forum NetBeansReplies: 1Last Post: 08212012, 03:32 PM 
Reverse
By system5634 in forum New To JavaReplies: 2Last Post: 03262012, 09:55 AM 
how to reverse string without using string reverse or array ?
By funkygarzon in forum New To JavaReplies: 10Last Post: 03152012, 11:46 AM 
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12072010, 07:23 AM 
Math.random in array and reverse order
By eugenechia in forum New To JavaReplies: 4Last Post: 02172010, 04:33 AM
Bookmarks