Skip to content

Instantly share code, notes, and snippets.

@neilfws
Created March 5, 2010 05:48
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 neilfws/322484 to your computer and use it in GitHub Desktop.
Save neilfws/322484 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
def collatz(n)
raise "#{n} does not compute" if n < 1
return n if n == 1
if n.odd?
n = 3 * n + 1
elsif n.even?
n = n/2
end
return n
end
x = ARGV[0].to_i
c = []
begin
x = collatz(x)
c << x
end until x == 1
puts c.join(","), "Steps = #{c.size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment