# Math Game Help

10 replies to this topic

### #1 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 29 January 2012 - 06:14 AM

I have a question. Is is possible to make Game Maker solve something like this:

A = 1 to 10;
B = 1 to 10;
C = 1 to 10;
D = 1 to 10;
A + or - or / or * B + or - or / or * C + or - or / or * D = 21;

The program should find if the equation is possible and if so, do you +,-,/, or, *.
I've been thinking but haven't go the answer. I think the only way is to check every way the equation can go individually, like 5+5.

I'd really appreciate if someone could find a less TEDIOUS option....Thanks in advance.
• 0
Portal 2 is awesomeness. Period.

### #2 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4353 posts
• Version:GM:Studio

Posted 29 January 2012 - 07:36 PM

To rephrase, you're asking whether or not the following situation can occur:

A, B, C, and D are unknown numbers from 1 to 10
We have a fifth number E that is randomly chosen.

Our goal is to have something of the form:

A _ B _ C _ D = E, where each blank is one of the four binary operations you have chosen above.

The goal is to find out if there is a systematic way of determining if there are numbers A, B, C, D and binary operations so that when filled into the above equation, the answer is E.

The answer to this question is that (1.) there is probably no systematic way of determining this, (2.) the solutions most likely aren't unique, (3.) depending on how you pick E, many solutions will not exist. The only way you can get an answer is to use brute force calculations by iterating over all 640,000 combinations; this is slow.

Instead, perhaps you can have an alternate design philosophy. Instead, pick the values of A, B, C, D along with the three binary operations, and then determine E from this. Then obscure the parameters that you want to hide and let the user fill these in. This way, you can quickly determine the solution (since you decided it from the start) and the user is left to fill in the details. This is much cleaner and more efficient than randomly picking an E value and hoping for the best.
• 0

### #3 DanRedux

DanRedux

GMC Member

• GMC Member
• 1403 posts
• Version:GM:Studio

Posted 29 January 2012 - 11:38 PM

There's a show called "Countdown" in England, and it has a Math Game portion exactly like this. The people are left to figure out the closest solution.

I've been desperately trying to solve this problem for a while- Now I have a second reason to, to help you, so maybe soon I'll crack it.

My current best guess was a Heuristic method... Basically, find which symbol gets you closest to the answer, treat it like pathfinding. It is very very fast but quite often can't find the solution. I think I will try backtracking, such that if the FIRST best solution doesn't work, go back to the SECOND. The worst-time for this is 640,000 iterations, the best time would be 4 iterations.

Edited by time4dan, 29 January 2012 - 11:39 PM.

• 0

### #4 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 09 February 2012 - 02:58 AM

Thank You for replying so quickly. I still have not figured this out. And yes it is A_B_C_D = E. A to D can be 1 to 10. I am open to ideas how to solve this if anyone else can. I hope we get an answer soon, for myself and other out there with this problem(which is probably not many....)
• 0
Portal 2 is awesomeness. Period.

GMC Member

• New Member
• 58 posts

Posted 12 February 2012 - 04:01 AM

I have a question. Is is possible to make Game Maker solve something like this:

A = 1 to 10;
B = 1 to 10;
C = 1 to 10;
D = 1 to 10;
A + or - or / or * B + or - or / or * C + or - or / or * D = 21;

The program should find if the equation is possible and if so, do you +,-,/, or, *.
I've been thinking but haven't go the answer. I think the only way is to check every way the equation can go individually, like 5+5.

I'd really appreciate if someone could find a less TEDIOUS option....Thanks in advance.

What you have there is an indeterminate equation (or more accurately, a family of indeterminate equations - since the operators are not set themselves, thus they can be seen as "unknowns" too); that means that, for a fixed/chosen set of operators (i.e. one member of the family), you'd have four unknowns A, B, C and D, but only one expression/relationship binding them together. The only way to solve such an equation is by trial and error: you give arbitrary values to three of those unknowns (which become parameters in your expression) and solve the resulting, determinate equation in the remaining unknown. You must understand this: [i]there is no precise, coherent way to determine a solution - or even whether a solution exists or not. Only trial and error.[i] Some optimizations are possible, of course, but not much... and it all depends on the values of E...

If I were to tackle this challenge myself, I would look at the following:
• Gather as much information as possible about the "known" result, E - in fact you should have told us more about it (e.g. is it always an integer, is it positive-only, does it have a lower and/or upper bound etc. etc.). Yes, I'd definitely look at E, maybe even factorize it and a few of its neighbours, just to get a better picture... Then use some educated guesses to work my way to that number.
• Erm... cheat! As in looking for alternative ways to achieving my ultimate goal, ways that avoid all this mucking around; I always found that the best way to solve a problem is to go around it (or not create it in the first place)... By the way, you should have told us what exactly do you need this for, maybe we can find a way to 'sidestep' past all this rubbish.
• Cheat again! Oh yeah, cheat my frikkin' ears off!! If I was after all the possible solutions to a given (sensible) value of E, then I would've gone for the brute force approach - which would be the simplest and most straightforward way of getting results. Except who says you need to do that at runtime?! Surely building a database of roughly half-a-mill entries is not *THAT* much of an inconvenience!... Especially considering the major benefit: all you have to do at runtime is a simple lookup and presto! you've got the answer.

All in all, nothing much to add to what xshortguy has already said - he nailed it pretty good...

Edited by NicAdi, 12 February 2012 - 04:04 AM.

• 0

### #6 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 12 February 2012 - 10:31 PM

Thank you for adding more sense to this problem for me. I really appreciate it.

I wanted to make kind of like a math game that gives you 4 random whole numbers between 1-10 that will equate to another whole number(21). The player is to try to find a way to incorporate all 4 numbers into the equation with addition, subtraction, multiplication, or division.

What I resulted to was to have the computer generate 4 random whole numbers and check if it is possible for 21, by using any of those 4 means necessary. If false, then generate another set to compute. Eventually it should get to 21. I am going to try it that way. Every time it succeeds, it would write it into a text file for future use. That means I don't have to do anything, but run this problem over and over again. Eventually, it'll get most of them, I hope.....
• 0
Portal 2 is awesomeness. Period.

### #7 DanRedux

DanRedux

GMC Member

• GMC Member
• 1403 posts
• Version:GM:Studio

Posted 12 February 2012 - 10:42 PM

Vastly overcomplicated.

Generate 4 numbers and 3 signs and calculate the answer, then just store the signs in memory.

4 x 5 + 2 - 5 = 17

Now hide the signs and you have yourself a good puzzle:

4 _ 5 _ 2 _ 5 = 17

Edited by time4dan, 12 February 2012 - 11:24 PM.

• 0

### #8 andykuo1

andykuo1

GMC Member

• GMC Member
• 29 posts

Posted 13 February 2012 - 12:09 AM

Awesome! Now I just have to....run the program multiple times before I get a decent amount of equations....

Thanks though!
• 0
Portal 2 is awesomeness. Period.

### #9 iam1me

iam1me

GMC Member

• GMC Member
• 380 posts
• Version:GM8

Posted 13 February 2012 - 12:23 AM

Awesome! Now I just have to....run the program multiple times before I get a decent amount of equations....

Thanks though!

Or just put a giant while(true) {...} loop in there, and let it run for a day
• 0
GUI API V1.0 is now here! http://gmc.yoyogames...0

Advanced Event Handling, Dynamic Interfaces, & Code Organization you can't get anywhere else using GML.

From a review by Creative Fusion:

It is extremely rare for me to comment, but all programmers that want to learn how to write professional code and everyone that wants a professional, easy-to-use, flexible, and FAST gui/hud/inventory/etc. system should support this project's development. I have been employed as a programmer (C++/CLI, C#, VisualBasic.NET, JavaScript, etc.) and know what companies look for and use, and I want to let people here know that they are using gui frameworks similar to what iam1me is providing here (if only gml were more object-oriented). For those that don't understand the current and massive potential benefits, I will try to help explain...

### #10 DanRedux

DanRedux

GMC Member

• GMC Member
• 1403 posts
• Version:GM:Studio

Posted 13 February 2012 - 01:01 AM

No, you should be generating these in-game, when the user requests a new map. Like I said, pick 4 numbers, combine with 3 signs, get the answer, then show the user everything but which signs you picked.
• 0

### #11 rude guss

rude guss

GMC Member

• GMC Member
• 236 posts

Posted 18 February 2012 - 06:47 AM

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.

• 0
Check out my Awesome 2d RTS Engine - It's Open-source, Unregistered and now has Multiplayer!!

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users