ruby - collatz-conjecture

This commit is contained in:
James Walker 2018-10-22 11:49:29 -04:00
parent fdb0f51e58
commit 0e1e6a0d9c
Signed by: walkah
GPG Key ID: 3C127179D6086E93

View File

@ -1,17 +1,11 @@
# CollatzConjecture class
class CollatzConjecture class CollatzConjecture
def self.steps(n, count = 0) def self.steps(num, count = 0)
if n < 1 raise ArgumentError if num < 1
raise ArgumentError return count if num == 1
elsif n == 1
count count += 1
else number = num.even? ? num / 2 : (3 * num) + 1
count += 1 steps(number, count)
if n % 2 == 0
number = n / 2
else
number = (3 * n) + 1
end
steps(number, count)
end
end end
end end