Skip to content

Instantly share code, notes, and snippets.

@johncrisostomo
Created May 27, 2011 22:20
Show Gist options
  • Save johncrisostomo/996317 to your computer and use it in GitHub Desktop.
Save johncrisostomo/996317 to your computer and use it in GitHub Desktop.
3n + 1
filename = ARGV
input = Array.new
output = Array.new
File.open(filename.first) do |f|
while line = f.gets
input << line.chomp!.split(" ")
end
end
input.each do |x, y|
max = 0
output << x + ' '
output << y + ' '
x.to_i.upto(y.to_i) do |i|
cycle = 1
while i != 1
if i.even?
i = i / 2
else
i = i * 3 + 1
end
cycle += 1
end
max = cycle if cycle > max
end
output << max.to_s + "\n"
end
puts output.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment