Skip to content

Instantly share code, notes, and snippets.

@banyan
Created December 24, 2009 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyan/263232 to your computer and use it in GitHub Desktop.
Save banyan/263232 to your computer and use it in GitHub Desktop.
class Collatz
def init(i)
buf = [i]
while i != 1
if (i % 2) == 0
i = i / 2
else
i = i * 3 + 1
end
buf << i
end
buf = [buf.size - 1, "[#{buf.join(', ')}]"]
end
end
require 'test/unit'
class TC_Collatz < Test::Unit::TestCase
def setup
@obj = Collatz.new
end
def test_init
assert_equal([6, "[10, 5, 16, 8, 4, 2, 1]"], @obj.init(10))
assert_equal([12, "[17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]"], @obj.init(17))
assert_equal([15, "[22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]"], @obj.init(22))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment