ruby - collatz-conjecture
This commit is contained in:
parent
fdb0f51e58
commit
0e1e6a0d9c
@ -1,17 +1,11 @@
|
||||
# CollatzConjecture class
|
||||
class CollatzConjecture
|
||||
def self.steps(n, count = 0)
|
||||
if n < 1
|
||||
raise ArgumentError
|
||||
elsif n == 1
|
||||
count
|
||||
else
|
||||
def self.steps(num, count = 0)
|
||||
raise ArgumentError if num < 1
|
||||
return count if num == 1
|
||||
|
||||
count += 1
|
||||
if n % 2 == 0
|
||||
number = n / 2
|
||||
else
|
||||
number = (3 * n) + 1
|
||||
end
|
||||
number = num.even? ? num / 2 : (3 * num) + 1
|
||||
steps(number, count)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user