Created
October 5, 2015 20:31
-
-
Save vznvzn/802102a6c33ff571d03b to your computer and use it in GitHub Desktop.
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
def f(n) | |
n = (n * 3 + 1) if (n.odd?) | |
n /= 2 while (n.even?) | |
return n | |
end | |
def pair(n) | |
return [n, f(n)] | |
end | |
def sum(l) | |
t = 0 | |
l.each { |x| t += x[0] } | |
return t | |
end | |
def select(l) | |
case ARGV[0] | |
when '0' | |
return (0...l.size).min_by { |x| (l[x][1] - l[x][0]).abs } | |
when '1' | |
return (0...l.size).max_by { |x| (l[x][1] - l[x][0]).abs } | |
end | |
end | |
n = 3 | |
l = [] | |
1000.times { l << pair(n); n += 2 } | |
t1 = sum(l) | |
seen = {} | |
i = 0 | |
begin | |
i += 1 | |
t = sum(l) | |
j = select(l) | |
z = l[j][1] | |
if (seen.member?(z)) | |
l.delete_at(j) | |
next | |
end | |
seen[z] = nil | |
l[j] = pair(z) | |
puts([t, l.size].join("\t")) | |
end while (!l.empty?) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment