This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Evil_Bengt's profile picture

I have no idea why this is happening...

Started by Evil_Bengt, 12 March 2015 - 01:43 PM
Evil_Bengt #1
Posted 12 March 2015 - 02:43 PM
Ok, I have a small problem that's not really related to computercraft only, it's more of a Lua thing. As I haven't got any Lua interpreter or similar installed on my computer I use Lua's demo thingy…. I've been messing around with quite an old idea of mine, to make a program that tests the "6174" thingy (for no reason at all). The thing with 6174 is that if you take a 4-digit number, arrange the integers from biggest to smallest and subtract with the opposite (smallest to biggest) you'll get a new number, then you do the same thing with that one. This goes on and on 'til you get any combination of 6174, if you do the same thing with that number, you'll just get 6174 again… This works for all 4-digit numbers (not 2222, 3333, 4444 etc…).

I made a program just for fun to test this, but there's one major issue, it just keep skipping the while-thingy, I've tried with an "if" and "else" and it skips to the else without doing anything.
The while should be triggered if "start" is NOT 6174, and to start with, it's 1234. But it behaves like it's the same number….

start = 1234
number = {}
while not start == 6174 do
number[4] = start - math.floor(start/10)*10;
number[3] = math.floor((start - math.floor(start/100)*100 - number[4])/10);
number[2] = math.floor((start - math.floor(start/1000)*1000 - number[4] - number[3]*10) / 100);
number[1] = math.floor(start/1000);
for i = 1, 4 do
  print(number[i])
end
table.sort(number)
largest = number[4]*1000 + number[3]*100 + number[2]*10 + number[1]
smallest = number[1]*1000 + number[2]*100 + number[3]*10 + number[4]
print(largest.. " - " ..smallest)
start = largest - smallest
end
print("Done!")
HDeffo #2
Posted 12 March 2015 - 03:03 PM
The issue is the while statement is still returning false instead you can format it this way

~= essentially means not equal to so it should still return what you are expecting it to


start = 1234
number = {}
while start ~= 6174 do
   number[4] = start - math.floor(start/10)*10;
   number[3] = math.floor((start - math.floor(start/100)*100 - number[4])/10);
   number[2] = math.floor((start - math.floor(start/1000)*1000 - number[4] - number[3]*10) / 100);
   number[1] = math.floor(start/1000);
   for i = 1, 4 do
	 print(number[i])
   end
   table.sort(number)
   largest = number[4]*1000 + number[3]*100 + number[2]*10 + number[1]
   smallest = number[1]*1000 + number[2]*100 + number[3]*10 + number[4]
   print(largest.. " - " ..smallest)
   start = largest - smallest
end
print("Done!")
Edited on 12 March 2015 - 02:04 PM
Evil_Bengt #3
Posted 12 March 2015 - 03:49 PM
The issue is the while statement is still returning false instead you can format it this way

~= essentially means not equal to so it should still return what you are expecting it to


start = 1234
number = {}
while start ~= 6174 do
   number[4] = start - math.floor(start/10)*10;
   number[3] = math.floor((start - math.floor(start/100)*100 - number[4])/10);
   number[2] = math.floor((start - math.floor(start/1000)*1000 - number[4] - number[3]*10) / 100);
   number[1] = math.floor(start/1000);
   for i = 1, 4 do
	 print(number[i])
   end
   table.sort(number)
   largest = number[4]*1000 + number[3]*100 + number[2]*10 + number[1]
   smallest = number[1]*1000 + number[2]*100 + number[3]*10 + number[4]
   print(largest.. " - " ..smallest)
   start = largest - smallest
end
print("Done!")
Thanx alot! Works like a charm! :D/> Still can't understand why "while not start == 6174 do" returns true when it should be false… What am I missing?
HDeffo #4
Posted 12 March 2015 - 03:57 PM
That part I can't answer sorry I've seen occasionally where not doesn't reverse the Boolean but I never understood when and why. As a side note with your code if I set it as whole
not start~=6174 do
It will always return true even when the start doesn't equal the number so for some reason under that logic

not + true = true
not + false = true
Edited on 12 March 2015 - 02:58 PM
Dragon53535 #5
Posted 12 March 2015 - 04:39 PM
That part I can't answer sorry I've seen occasionally where not doesn't reverse the Boolean but I never understood when and why. As a side note with your code if I set it as whole
not start~=6174 do
It will always return true even when the start doesn't equal the number so for some reason under that logic

not + true = true
not + false = true
Wrong, the reason why it's not working correctly, is that

not start == 6174
is interpreted as

(not start) == 6174
Start is being inverted, so it's saying, while the opposite of start == 6174 do, which won't work. What you want is to do this: (Besides the ~=, that's a better solution anyways)

while not (start == 6174) do
In which the boolean value returned by start == 6174 is reversed.


Ps: Not + true = FALSE
not + false = TRUE

That will never change.
Edited on 12 March 2015 - 03:40 PM
HDeffo #6
Posted 12 March 2015 - 05:19 PM
-snip-
It's not really wrong as I specifically stated I really didn't know why it was reversing it so strange :P/> and as for the strange logic I know not+false=true and not+true=false I was merely pointing out how it seemed strange how it was returning which didn't fit how I figured the return should be. Yes you are right now that you've pointed it out the reasoning seems rather obvious on it :P/>
Evil_Bengt #7
Posted 01 April 2015 - 05:13 PM
Niiiice! Thanks guys! :)/>

Does anyone have a better and maybe more "modular" solution to this then:


number[4] = start - math.floor(start/10)*10
number[3] = math.floor((start - math.floor(start/100)*100 - number[4])/10)
number[2] = math.floor((start - math.floor(start/1000)*1000 - number[4] - number[3]*10) / 100)
number[1] = math.floor(start/1000)

This is just the part that takes the 4-digit number and splits it into 4 separate variables.
My version doesn't seem too effective and it looks like shit so maybe someone has a better idea? :)/>
Dragon53535 #8
Posted 01 April 2015 - 06:43 PM
Easy, you can convert to string, sub out, and then convert back.


local strNum = tostring(start)
for i = 1, 4 do
  number[i] = strNum:sub(i,i)
end

Assuming that number[1] is the thousands place. this should work.
KingofGamesYami #9
Posted 01 April 2015 - 07:22 PM

local number = {}
for num in tostring( strNum ):gmatch( "%d" ) do
  number[ #number + 1 ] = tonumber( num )
end

Because I like patterns, and I like things that work no matter what.

Also, if you want to do it from right to left, just stick a :reverse() after tostring.
Edited on 01 April 2015 - 05:25 PM