Created
May 27, 2011 22:20
-
-
Save johncrisostomo/996317 to your computer and use it in GitHub Desktop.
3n + 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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