This code is not working. I'm trying to code the Collatz conjecture but the code only seems to run once for the input 8. It prints out 4, 1 ,1 and so that shows it only runs for one step. The else block is never executed either. Can someone tell me what's wrong with this Ruby code? I have no idea why it's not working the way it's supposed to.
class Collatz
def collatz(number)
if number == 1
return 0
end
steps = 0
while number > 1 do
if number % 2 == 0
number = number / 2
puts number
steps = steps + 1
puts steps
else
number = (number * 3) + 1
puts number
steps = steps + 1
puts steps
end
return steps
end
end
steps = Collatz.new.collatz(8)
puts steps
end