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
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