Problem 14

Ruby

 1def get_n_chains(orig_start, last)
 2    start = orig_start
 3    max = 0
 4    chain = 0
 5    current_chain = 0;
 6    loop do
 7        if start == last - 1 
 8            break
 9        end
10        current_elem = start
11        chain = 0
12        loop do
13            if current_elem % 2 == 0
14                current_elem = current_elem/2
15                chain += 1
16                if current_elem == 1
17                    break
18                end
19            else
20                current_elem = (3*current_elem) + 1
21                chain += 1
22            end
23        end
24        if current_chain < chain
25            current_chain = chain
26            max = start 
27        end
28        start = start - 1
29    end
30    return max, current_chain
31end
32
33a, b, c, d, e, f, g= [
34    Thread.new{get_n_chains(1000000, 850000)},
35    Thread.new{get_n_chains(849999, 700000)},
36    Thread.new{get_n_chains(699999, 550000)},
37    Thread.new{get_n_chains(549999, 400000)},
38    Thread.new{get_n_chains(399999, 250000)},
39    Thread.new{get_n_chains(249999, 100000)},
40    Thread.new{get_n_chains(99999, 10)},
41
42].map(&:join).map(&:value)
43puts"#{[a, b, c, d, e, f, g][1].max}"