Reputation: 197
My math teacher has an extra credit halloween problem that goes like this:
each letter represents a digit 2-9, and you need the following problem to work:
trick + or = treat
I decided I want to find ALL possible solutions to the problem (To impress him) so I decide to write a computer program that would tell me the all the answers. Here is my following code:
local function checkAdd()
local trick =k+(10*c)+(100*i)+(1000*r)+(10000*t) local _or =r+(10*o) local treat = t+(10*a)+(100*e)+(1000*r)+(10000*t) if trick + _or == treat then print(trick) print(" ".._or) print(treat) print(t) print(r) print(i) print(c) print(k) print(o) print(e) print(a) end --print("end") timer.performWithDelay(1,newNumbers) end local function checkNumbers8() if t or r or i or c or k or o or e or a == "9" then checkAdd() else newNumbers() end end
local function checkNumbers7() if t or r or i or c or k or o or e or a == "8" then checkNumbers8() else newNumbers() end end
local function checkNumbers6() if t or r or i or c or k or o or e or a == "7" then checkNumbers7() else newNumbers() end end
local function checkNumbers5() if t or r or i or c or k or o or e or a == "6" then checkNumbers6() else newNumbers() end end
local function checkNumbers4() if t or r or i or c or k or o or e or a == "5" then checkNumbers5() else newNumbers() end end
local function checkNumbers3() if t or r or i or c or k or o or e or a == "4" then checkNumbers4() else newNumbers() end end
local function checkNumbers2() if t or r or i or c or k or o or e or a == "3" then checkNumbers3() else newNumbers() end end
local function checkNumbers() if t or r or i or c or k or o or e or a == "2" then checkNumbers2() else newNumbers() end end
function newNumbers() t = mRandom(2,9) r = mRandom(2,9) i = mRandom(2,9) c = mRandom(2,9) k = mRandom(2,9) o = mRandom(2,9) e = mRandom(2,9) a = mRandom(2,9) checkNumbers() end
newNumbers()
*Please note that on in the function checkAdd I call the function timer.performwithdelay ( waits 1 milisecond before calling the function). This is because if I run this code just regularly without the function call, I get a stack overflow error. So i put my code into a framework I use for app developement that had the timer.performwithdelay call, and I implemented that into my code so the computer won't be so overwhelmed and cause an overflow error.
I get the following print statements:
97552
27
97579
9
7
5
5
2
2
5
7
and:
49325
59
49384
4
9
3
2
5
5
3
8
I am getting some letters equaling the same as other letters! and not all numbers 2-9 are used! What is wrong with my code? I test to see if every number 2-9 is used
Upvotes: 0
Views: 94
Reputation: 26744
I think there are two main issues. I don't see the function mRandom
, but I assume it returns a number and you are comparing it with a string. Number 2 is not the same as string '2', so 2 == '2'
returns false
.
The second issue is that (it seems) you are trying to compare if either of variables has a specific value, but you can't do if a or b == 2 then
meaning: execute if either a
or b
equals 2. For that you need to write if a == 2 or b == 2
. What you have is evaluated as: if a
is evaluated as true
(which is when it's not nil
or false
) or b
equals 2.
Upvotes: 2