Skip to content

Instantly share code, notes, and snippets.

@rodriguezd
Created August 22, 2013 14:21
Show Gist options
  • Save rodriguezd/6307824 to your computer and use it in GitHub Desktop.
Save rodriguezd/6307824 to your computer and use it in GitHub Desktop.
class Collatz
def initialize(number)
@sequence_num = number
@sequence_array = []
@sequence_array << @sequence_num
end
def gen_sequence
until @sequence_num == 1
if @sequence_num.even?
@sequence_num = @sequence_num / 2
else
@sequence_num = 3*@sequence_num + 1
end
@sequence_array << @sequence_num
end
@sequence_array
end
end
start_num = 0
max_length = 0
(2..999999).each do |num|
result_array = Collatz.new(num).gen_sequence
if result_array.length > max_length
max_length = result_array.length
start_num = num
end
end
puts start_num
puts max_length
@wengzilla
Copy link

Consider on L3 (line 3) doing @sequence_array = [@sequence_num], which should replace L4
Consider breaking L11 through L15 into a method called next_step

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment