Created
August 22, 2013 14:21
-
-
Save rodriguezd/6307824 to your computer and use it in GitHub Desktop.
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
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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