The Collatz conjecture:
https://en.wikipedia.org/wiki/Collatz_conjecture
For any positive integer n we define two rules: if even: divide by two if odd: multiply by three, then add one, and repeat until the result is the number 1. The smallest value of n is 1.
This will generate sequences of numbers like below, converging to 1:
6, 3, 10, 5, 16, 8, 4, 2, 1
For each number n we can now count the number of steps in this sequence until we reach 1.
So the sequence above, starting with 6, has a length of 9 (including the starting point and the final one).
My problem:
I am trying to find the second-longest sequence of all the integers smaller or equal than 10 million. How would you go about it?
So far I came up with a solution for finding the longest sequence, but I am not sure how to find the 2nd.
def collatz_sequence_eval(n)
array_sequence = []
until n == 1
if n%2 != 0
n = 3*n + 1
array_sequence.push(n)
else
n = n/2
array_sequence.push(n)
end
end
return array_sequence
end
def collatz_iterator
counter = 1
current_longest_sequence = []
until counter == 10000000
cur_seq = collatz_sequence_eval(counter)
if cur_seq.length > current_longest_sequence.length
current_longest_sequence = cur_seq
counter+=1
else
counter+=1
end
end
puts "Starting number is #{current_longest_sequence[0]}.
Sequence length is #{current_longest_sequence.length}"
end