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