Created
May 20, 2013 15:39
-
-
Save jcbwlkr/5613032 to your computer and use it in GitHub Desktop.
Submission for the 2013-05-17 UpFront Wichita code challenge.
This file contains 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
#!/usr/bin/env ruby | |
class CycleCalc | |
def initialize(lowestN, highestN) | |
@lowestN, @highestN = lowestN.to_i, highestN.to_i | |
end | |
def maxLength | |
max = 0 | |
for n in @lowestN..@highestN | |
l = cycleLength n | |
max = l if l > max | |
end | |
max | |
end | |
def cycleLength(n) | |
@length = 1 | |
transform n | |
@length | |
end | |
def transform(n) | |
return if n == 1 | |
@length = @length + 1 | |
if n % 2 === 0 | |
n = n / 2 | |
else | |
n = n * 3 + 1 | |
end | |
transform n unless n == 1 | |
end | |
end | |
c = CycleCalc.new(ARGV[0], ARGV[1]) | |
puts ARGV[0] + " " + ARGV[1] + " " + c.maxLength.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment