Collatz Conjecture Visualization

I saw a Veritasium (Youtuber) video, about the Collatz Conjecture:

  1. Pick a number.
  2. If the number is odd, multiply by 3 and add 1. If the number is even, divide by 2.

And continue, and continue, and you will always, in the end, get “1”.

I wanted to visualize it in studio, so I did! Not without a little struggle, but it’s getting there.

You can make your own, and see how it works in studio with this code.


local function Even(num)
	return (num % 2 == 0)
end

local function StartGraph(numberToStart)
	local Number = numberToStart
	while true do --Do this infinitely
		if Even(Number) then --If it is even
			Number /= 2  --Divides the number by 2
		else --If it's not even
			Number = (Number*3)+1 --Multiplies by 3 and then adds 1. 
		end 
		print(Number) --Prints the current Number
		if Number == 1 then --If the number reached 1
			print("It reached one.") --Prints
			break --Ends the loop
		end
	end
end

I plan on doing a some more things to improve it, such as some lines, and fixing some scaling issues, but otherwise, thoughts?

I see no need for this
make it

else
1 Like