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
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.
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.
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…
And now…OMG, there gotta be some kind of math formula for this…
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
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.
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.