# 3x+1 with Lua with Explanation ~ Collatz Conjecture

### 3x+1, the simplest equation but unsolvable.

Today, we are going to be making a 3x+1 calculator by assuming x as a number.

If you don’t know what 3x+1 is, go check this video out.

Okay, first we going to say x is 8.

``````local x = 8
print(x)
``````

Now, we have to make a while do statement if x is greater than 1

``````while x > 1 do
``````

Firstly, we need to make an if statement.

``````if(x % 2) == 0 then
``````

Now, we have to half x.

``````x = x / 2
print(x)
``````

Lastly, we make an else statement.

``````else
x = x * 3 + 1
print(x)
``````

Now, if you run it, the console will say 8, 4, 2, 1.
You can change `local x = 8` to any number you want and it will end in a result of 1 no matter what.

If you want to watch my youtube video, I forgot two things in the video. On line 8, make a new line and type in `print(n)` . On line one, make a new line that says, `print(n)` . Sorry for the mistakes. (Just didn’t want to record it again)

12 Likes

The tutorial we didn’t know we needed. Thanks!

3 Likes

This is a very interesting topic. I made a Collatz Conjecture program in Lua a while back after being introduced to it by my maths teacher, although I extended it a fair bit more than this to actually search numbers and iterate through to try and find a number that didn’t end in the 4, 2, 1 loop. I started it from the highest number that had been previously checked (which is 295147905179352825856 from a quick google search). Never got a number that worked after leaving it to chug for about a week, but was interesting nonetheless. Here’s the code (for anyone interested ). I didn’t use Roblox for this, which is why I don’t use table functions such as clear.

Code
``````local testingNumber = 295147905179352825856
local workingNumber = 0
local workingTable = {}
local found = false

while true do
for i, v in pairs(workingTable) do
workingTable[i] = nil
end
if found == false then
local foundLoop = false
testingNumber = testingNumber + 1
workingNumber = testingNumber
print("testing " .. testingNumber)
repeat
table.insert(workingTable, workingNumber)
if workingNumber % 2 == 0 then
workingNumber = workingNumber / 2
else
workingNumber = (workingNumber * 3) + 1
end
for i, v in pairs(workingTable) do
if v == workingNumber then
if v == 4 or v == 2 or v == 1 then
foundLoop = true
else
foundLoop = true
found = true
print(testingNumber .. " WORKS!!!!!!!!!!!!!!!!!")
end
end
end
until foundLoop == true
else
print(testingNumber .. "WORKS!!!!!!!!!!!!!!!!!")
end
end
``````
2 Likes

``````local bestNum = 1

function calc(n, nFormed) -- 3x+1 calculation
if n <= 1 then
return nFormed
end

if n % 2 ~= 0 then
n = (n * 3) + 1
else
n = n / 2
end

return calc(n, nFormed + 1)
end

for i = 1, 1000000000000 do
local val = calc(i, 1)
if val > bestNum then
bestNum = val  -- save best result as highest val from calculation
print(i, bestNum, "numbers formed")
end
end
``````

and here are the formatted data I collected over a long period of running this code:

``````number: 1 | formed 0 total numbers
number: 2 | formed 1 total numbers
number: 3 | formed 7 total numbers
number: 6 | formed 8 total numbers
number: 7 | formed 16 total numbers
number: 9 | formed 19 total numbers
number: 18 | formed 20 total numbers
number: 25 | formed 23 total numbers
number: 27 | formed 111 total numbers
number: 54 | formed 112 total numbers
number: 73 | formed 115 total numbers
number: 97 | formed 118 total numbers
number: 129 | formed 121 total numbers
number: 171 | formed 124 total numbers
number: 231 | formed 127 total numbers
number: 313 | formed 130 total numbers
number: 327 | formed 143 total numbers
number: 649 | formed 144 total numbers
number: 703 | formed 170 total numbers
number: 871 | formed 178 total numbers
number: 1,161 | formed 181 total numbers
number: 2,223 | formed 182 total numbers
number: 2,463 | formed 208 total numbers
number: 2,919 | formed 216 total numbers
number: 3,711 | formed 237 total numbers
number: 6,171 | formed 261 total numbers
number: 10,971 | formed 267 total numbers
number: 13,255 | formed 275 total numbers
number: 17,647 | formed 278 total numbers
number: 23,529 | formed 281 total numbers
number: 26,623 | formed 307 total numbers
number: 34,239 | formed 310 total numbers
number: 35,655 | formed 323 total numbers
number: 52,527 | formed 339 total numbers
number: 77,031 | formed 350 total numbers
number: 106,239 | formed 353 total numbers
number: 142,587 | formed 374 total numbers
number: 156,159 | formed 382 total numbers
number: 216,367 | formed 385 total numbers
number: 230,631 | formed 442 total numbers
number: 410,011 | formed 448 total numbers
number: 511,935 | formed 469 total numbers
number: 626,331 | formed 508 total numbers
number: 837,799 | formed 524 total numbers
number: 1,117,065 | formed 527 total numbers
number: 1,501,353 | formed 530 total numbers
number: 1,723,519 | formed 556 total numbers
number: 2,298,025 | formed 559 total numbers
number: 3,064,033 | formed 562 total numbers
number: 3,542,887 | formed 583 total numbers
number: 3,732,423 | formed 596 total numbers
number: 5,649,499 | formed 612 total numbers
number: 6,649,279 | formed 664 total numbers
number: 8,400,511 | formed 685 total numbers
number: 11,200,681 | formed 688 total numbers
number: 14,934,241 | formed 691 total numbers
number: 15,733,191 | formed 704 total numbers
number: 31,466,382 | formed 705 total numbers
number: 36,791,535 | formed 744 total numbers
number: 63,728,127 | formed 949 total numbers
number: 127,456,254 | formed 950 total numbers
-----------------------------------------------
lua + luajit results (quicker processing time)
-----------------------------------------------
number: 169,941,673 | formed 954 total numbers
number: 226,588,897 | formed 957 total numbers
number: 268,549,803 | formed 965 total numbers
number: 537,099,606 | formed 966 total numbers
number: 670,617,279 | formed 987 total numbers
number: 1,341,234,558 | formed 988 total numbers
number: 1,412,987,847 | formed 1001 total numbers
number: 1,674,652,263 | formed 1009 total numbers
number: 2,610,744,987 | formed 1051 total numbers
number: 4,578,853,915 | formed 1088 total numbers
number: 4,890,328,815 | formed 1132 total numbers
``````

Edit: I do realise this is a year old post but its relevant and interesting - thanks for whoever reads this.

2 Likes