• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Best method to find sum of x for three numbers.

Gamebot

Member
If I add three posative numbers to equal 19, keeping in mind 19 can be any number, I would like to know the best way to accomplish this for all possible numbers.

As of right now I have three for loops that simply check if: I + J + K = x. When x in this case is 19. It works fine and numbers wont go over 30 for my use. I have to wonder if checking that many values is the best way of doing it.

Thought someone with much better math skills especially in programming might have a better answer.
 

obscene

Member
Your computer does not see 3 values and think, "whoa, that's a lot of values." :) It's pretty good at math. Addition is it's favorite.

But what are three for-loops about? If there's a better way at all, we'd have to see all the relevant code.
 

TheouAegis

Member
Is each number unique? So that no two numbers could be the same, such as the set [5,9,5]? Do you care what order the numbers are in (in other words, if it finds [4,5,10], should it ignore [4,10,5] and other such combos)? Right now your code finds all permutations. Or are you just trying to pick 3 numbers at random and see if they add up to the target value? Or are you trying to, say, have 5 numbers chosen at random such that only 3 of them can possibly add up to the target value?
 

Gamebot

Member
I really need to learn how to explain myself better.

I need all possibilities in sequence as it is for looking up values within a 3d grid. (Still working a few things out)

I did figure out I needed to change the formula a bit. Because I should have known better. :confused:


Instead of this:
a + b + c = x

I need:
b + c - a = x

Therfore with a 5 x 5 grid looking up all values for the number 3. I would have:

113
122
131
214
223
.
.
544

It should be in that order also.
Loops will work fine for now.
 
Last edited:
Top