Results 1 to 7 of 7

Thread: Reverse Math?

  1. #1
    LG87 is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reverse Math?

    Quote Originally Posted by KevinWorkman View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kimi is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: Reverse Math?

    random numbers and random operators? That seems a kinda hard problem... you should probably try with backtracking or memoization...

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default 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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Reverse Math?

    yep!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Reverse a String
    By Mehwish-S in forum NetBeans
    Replies: 1
    Last Post: 08-21-2012, 03:32 PM
  2. Reverse
    By system5634 in forum New To Java
    Replies: 2
    Last Post: 03-26-2012, 09:55 AM
  3. Replies: 10
    Last Post: 03-15-2012, 10:46 AM
  4. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  5. Math.random in array and reverse order
    By eugenechia in forum New To Java
    Replies: 4
    Last Post: 02-17-2010, 03:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •