diff --git a/ruby/collatz-conjecture/collatz_conjecture.rb b/ruby/collatz-conjecture/collatz_conjecture.rb index 939762f..d891b13 100644 --- a/ruby/collatz-conjecture/collatz_conjecture.rb +++ b/ruby/collatz-conjecture/collatz_conjecture.rb @@ -1,17 +1,11 @@ +# CollatzConjecture class class CollatzConjecture - def self.steps(n, count = 0) - if n < 1 - raise ArgumentError - elsif n == 1 - count - else - count += 1 - if n % 2 == 0 - number = n / 2 - else - number = (3 * n) + 1 - end - steps(number, count) - end + def self.steps(num, count = 0) + raise ArgumentError if num < 1 + return count if num == 1 + + count += 1 + number = num.even? ? num / 2 : (3 * num) + 1 + steps(number, count) end end