From 0e1e6a0d9cf49185a064ea85c4a54ea1da156d90 Mon Sep 17 00:00:00 2001 From: James Walker Date: Mon, 22 Oct 2018 11:49:29 -0400 Subject: [PATCH] ruby - collatz-conjecture --- ruby/collatz-conjecture/collatz_conjecture.rb | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) 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