How to find all combos with sum of 10

If i have numbers 1-9 and want to find all combos that adds up to the sum of 10 using +/-, how is that done?

I started doing it manually on paper… :wacko:

What is it called when you find combos like this?

1 + 9 = 10

1 + 2 + 7 = 10

1 + 3 + 6 = 10

1 + 4 + 5 = 10

1 + 5 + 4 = 10

1 + 6 + 3 = 10

1 + 7 + 2 = 10

1 + 2 + 3 + 4 = 10

1 + 6 + 8 - 5 = 10

etc…

A number can only be used once so…

1+1+8 = 10

is not a valid combo.

Unsure what to google for… :unsure:

Thanks

Still don’t know what this is called i programming terms but I made some progress… Any code “wizards” out there who could chime in…??

[lua]

–[[

1 + 9 = 10

1 + 2 + 7 = 10

1 + 3 + 6 = 10

1 + 4 + 5 = 10

1 + 5 + 4 = 10

1 + 6 + 3 = 10

1 + 7 + 2 = 10

–]]

local combos = {}

local numbers = {1,2,3,4,5,6,7,8,9}

local SUM = 10

local idx = #numbers

local valid = false

local result = nil

local aCombo = nil

local function findCombo()

       local count = 1

       for i = 1, #numbers, 1 do

             result = numbers[count] + numbers[idx]

             if result == SUM then 

                    aCombo = tostring(numbers[count] …" + “…numbers[idx])…” = 10"

                    valid = true 

            end

           

           idx = idx - 1

           if idx == 1 then 

               idx = #numbers 

               count = count + 1

           end

      end

      if valid and not combos[aCombo] then

            combos[#combos + 1] = aCombo

           valid = false

           print(" Combo : "…aCombo)

      end

end

findCombo()

[/lua]

local usage = { 0,0,0,0,0,0,0,0,0 } -- in each position, 0 not used, 1 +ve, 2 -ve. local count = 0 repeat local total = 0 -- calculate total for \_,n in ipairs(usage) do if n == 1 then total = total + \_ end if n == 2 then total = total - \_ end end if total == 10 then local sum = "" for \_,n in ipairs(usage) do if n == 1 then sum = sum .. "+" .. \_ end if n == 2 then sum = sum .. "-" .. \_ end end print(sum:sub(2)) count = count + 1 end local i = 1; local carry; repeat usage[i] = usage[i] + 1 carry = false if usage[i] == 3 then carry = true usage[i] = 0 end i = i + 1 until not carry or i == 10 until i == 10 and carry print(count)

This is one method.

The idea is that each ‘usage’ of each digit (+,- or not used) is represented by 3 values  0,1,2 and these are kept in a base 3 number stored in usage - so if usage[4] = 2 then -4 is added to the total.

The code is fairly straightforward - 

  • firstly work out the current total
  • if it is 10, convert the ‘used’ numbers to a string and display it.
  • increment the base 3 counter.

You could probably do it with modulus 3 but it would be more confusing.

Algorithms like this are contextual - it partly depends on what you are trying to do :slight_smile:

love these sorts of puzzlers, and paul offers a clever solution.

paul:  what if your usage values were -1,0,1 instead of 0.1,2?  (see it?) (total=total+(n-1)*_  -ish?)

cheers! :slight_smile:

I didn’t want it to be too hard to understand. You could do it by counting from 1 to 3^9 and repeatedly dividing by 3 and using the modulus 3 - 1 and that would be much quicker but less understandable.

Hmmm…

Trying to understand the code…

But that is not entirely true, if i just run the algorithm with addition then it return 9 combos:

1+2+3+4=10

2+3+5=10

1+4+5=10

1+3+6=10

4+6=10

1+2+7=10

3+7=10

2+8=10

1+9=10

But if I do it manually by “pen and paper” i get more results

1 + 5 + 4 = 10

1 + 6 + 3 = 10

1 + 7 + 2 = 10

So what I’m saying is and my thought was to find all combos starting with 1 then all combos starting with 2 etc

so i short…

– 2 number combo = 10

1 + 9 = 10

– 3 number combo = 10

1 + 2 + 7 = 10

1 + 7 + 2 = 10

1 + 3 + 6 = 10

1 + 6 + 3 = 10

1 + 4 + 5 = 10

1 + 5 + 4 = 10

– 4 number combo = 10

1 + 2 + 3 + 4 = 10

1 + 3 + 4 + 2 = 10

1 + 4 + 2 + 3 = 10

1 + 2 + 4 + 3 = 10

1 + 3 + 2 + 4 = 10

…etc

2 + 8 = 10

…etc

and like I mention earlier a number can only be used once in a combo

1 + 1 + 8 = 10 

would not be valid

I also added division and multiplication but then I get duplicates.

I insert the combo in a table like this

[lua]

– … code…

print(sum:sub(2))

local combo = {}

if not combo[tostring( sum:sub(2) ) ] then

       combo[#combo + 1] = tostring( sum:sub(2) )

end

count = count + 1

[/lua]

this should only insert the value if it is not already a value in my table, right?

I was assuming 1+4+5 and 1+5+4 were the same ; obviously if not then you have all the permutations of the terms. Can I ask what you want this for ? Some sort of maths game ?

If you include the permutations and minuses there will be a lot , because if you run the lua script above you will see there are some 7 and 8 entry terms and they will have 8! permutations.

If this is for a quiz, you could do it by random trial and error, e.g. keep picking combinations until  you find one that adds up to ten, which might be easier and more flexible.

i’d have assumed same.  at this point, already given the combinations, maybe just run them each through Johnson-Trotter for the (many) permutations.

It’s not a part of a game or anything, while I was at the ATM I though of this. Numpads, calculators etc have number 0-9 keys, wonder how many combos there are, then a bit later…

…combos with numbers only used once…then a bit later…

hmmm…what if…since its 10 keys/numbers, shuffle/add so the sum is 10 then things kinda evolved and now its a bit out of hand…

TOO BIG OF A TASK… :wacko:

…perhaps… :slight_smile:

And now…OMG, there gotta be some kind of math formula for this…

there is, but you’ll get better “hits” if you drop the term “combo” (combination) and adopt the term “permutation”

fe:  atm, 4 of 10, unordered w repetition, simple case:  10*10*10*10

fe:  lotto:  5 of 49, unordered no repetition, 49*48*47*46*45

and so on…

use “combination” only for cases where order doesn’t mater (fe 1234 same as 4321, but differs from 1235,5321)

that’ll help you get the math geeks on the right track  :D

hth

“permutation”…check!

Still don’t know what this is called i programming terms but I made some progress… Any code “wizards” out there who could chime in…??

[lua]

–[[

1 + 9 = 10

1 + 2 + 7 = 10

1 + 3 + 6 = 10

1 + 4 + 5 = 10

1 + 5 + 4 = 10

1 + 6 + 3 = 10

1 + 7 + 2 = 10

–]]

local combos = {}

local numbers = {1,2,3,4,5,6,7,8,9}

local SUM = 10

local idx = #numbers

local valid = false

local result = nil

local aCombo = nil

local function findCombo()

       local count = 1

       for i = 1, #numbers, 1 do

             result = numbers[count] + numbers[idx]

             if result == SUM then 

                    aCombo = tostring(numbers[count] …" + “…numbers[idx])…” = 10"

                    valid = true 

            end

           

           idx = idx - 1

           if idx == 1 then 

               idx = #numbers 

               count = count + 1

           end

      end

      if valid and not combos[aCombo] then

            combos[#combos + 1] = aCombo

           valid = false

           print(" Combo : "…aCombo)

      end

end

findCombo()

[/lua]

local usage = { 0,0,0,0,0,0,0,0,0 } -- in each position, 0 not used, 1 +ve, 2 -ve. local count = 0 repeat local total = 0 -- calculate total for \_,n in ipairs(usage) do if n == 1 then total = total + \_ end if n == 2 then total = total - \_ end end if total == 10 then local sum = "" for \_,n in ipairs(usage) do if n == 1 then sum = sum .. "+" .. \_ end if n == 2 then sum = sum .. "-" .. \_ end end print(sum:sub(2)) count = count + 1 end local i = 1; local carry; repeat usage[i] = usage[i] + 1 carry = false if usage[i] == 3 then carry = true usage[i] = 0 end i = i + 1 until not carry or i == 10 until i == 10 and carry print(count)

This is one method.

The idea is that each ‘usage’ of each digit (+,- or not used) is represented by 3 values  0,1,2 and these are kept in a base 3 number stored in usage - so if usage[4] = 2 then -4 is added to the total.

The code is fairly straightforward - 

  • firstly work out the current total
  • if it is 10, convert the ‘used’ numbers to a string and display it.
  • increment the base 3 counter.

You could probably do it with modulus 3 but it would be more confusing.

Algorithms like this are contextual - it partly depends on what you are trying to do :slight_smile:

love these sorts of puzzlers, and paul offers a clever solution.

paul:  what if your usage values were -1,0,1 instead of 0.1,2?  (see it?) (total=total+(n-1)*_  -ish?)

cheers! :slight_smile:

I didn’t want it to be too hard to understand. You could do it by counting from 1 to 3^9 and repeatedly dividing by 3 and using the modulus 3 - 1 and that would be much quicker but less understandable.

Hmmm…

Trying to understand the code…

But that is not entirely true, if i just run the algorithm with addition then it return 9 combos:

1+2+3+4=10

2+3+5=10

1+4+5=10

1+3+6=10

4+6=10

1+2+7=10

3+7=10

2+8=10

1+9=10

But if I do it manually by “pen and paper” i get more results

1 + 5 + 4 = 10

1 + 6 + 3 = 10

1 + 7 + 2 = 10

So what I’m saying is and my thought was to find all combos starting with 1 then all combos starting with 2 etc

so i short…

– 2 number combo = 10

1 + 9 = 10

– 3 number combo = 10

1 + 2 + 7 = 10

1 + 7 + 2 = 10

1 + 3 + 6 = 10

1 + 6 + 3 = 10

1 + 4 + 5 = 10

1 + 5 + 4 = 10

– 4 number combo = 10

1 + 2 + 3 + 4 = 10

1 + 3 + 4 + 2 = 10

1 + 4 + 2 + 3 = 10

1 + 2 + 4 + 3 = 10

1 + 3 + 2 + 4 = 10

…etc

2 + 8 = 10

…etc

and like I mention earlier a number can only be used once in a combo

1 + 1 + 8 = 10 

would not be valid

I also added division and multiplication but then I get duplicates.

I insert the combo in a table like this

[lua]

– … code…

print(sum:sub(2))

local combo = {}

if not combo[tostring( sum:sub(2) ) ] then

       combo[#combo + 1] = tostring( sum:sub(2) )

end

count = count + 1

[/lua]

this should only insert the value if it is not already a value in my table, right?

I was assuming 1+4+5 and 1+5+4 were the same ; obviously if not then you have all the permutations of the terms. Can I ask what you want this for ? Some sort of maths game ?

If you include the permutations and minuses there will be a lot , because if you run the lua script above you will see there are some 7 and 8 entry terms and they will have 8! permutations.

If this is for a quiz, you could do it by random trial and error, e.g. keep picking combinations until  you find one that adds up to ten, which might be easier and more flexible.

i’d have assumed same.  at this point, already given the combinations, maybe just run them each through Johnson-Trotter for the (many) permutations.