Last active
November 25, 2015 17:25
-
-
Save vznvzn/aeab5ea20daafb18c31c 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 fs() | |
lo = [lambda { |n| n * 3 + 1 }, lambda { |n| (n * 3 + 1) / 2 }, | |
lambda { |n| n = (n * 3 + 1) / 2 while (n.odd?); n }] | |
le = [lambda { |n| n / 2 }, lambda { |n| n /= 2 while (n.even?); n }] | |
l = [] | |
lo.each \ | |
{ | |
|o| | |
le.each \ | |
{ | |
|e| | |
l << lambda \ | |
{ | |
|n| | |
n = o.call(n) if (n.odd?) | |
n = e.call(n) if (n.even?) | |
n | |
} | |
} | |
} | |
return l | |
end | |
def sum(l) | |
t = 0 | |
l.each { |x| t += x } | |
return t | |
end | |
def smooth(f, c) | |
n = n1 = 1001 | |
l = [] | |
500.times { l << [n, n2 = f.call(n), n2 - n]; n += 2 } | |
seen = {1=>nil} | |
y = t2 = m = 0 | |
begin | |
j = (0...l.size).min_by { |x| l[x][2] } | |
if (l[j][2] > 0) then | |
z = l.delete_at(j) | |
if (!seen.member?(z[1])) then | |
seen[z[1]] = nil | |
l << [z[1], n2 = f.call(z[1]), n2 - z[1]] | |
end | |
else | |
l2 = (0...l.size).select { |x| l[x][2] < 0 }.sort_by { |x| l[x][2] }.reverse | |
tn = sum(l2.map { |x| l[x][2] }) | |
j = (0...l.size).select { |x| l[x][2] + tn < 0 }.max_by { |x| l[x][2] } | |
tp = l[j][2] | |
i = 0 | |
begin | |
if (tp + (tn - l[l2[i]][2]) < 0) then | |
tn -= l[l2[i]][2] | |
l2.delete_at(i) | |
next | |
end | |
i += 1 | |
end while (i < l2.size) | |
l2 << j | |
l2.sort.reverse.each \ | |
{ | |
|x| | |
z = l.delete_at(x) | |
next if (seen.member?(z[1])) | |
seen[z[1]] = nil | |
l << [z[1], n2 = f.call(z[1]), n2 - z[1]] | |
} | |
end | |
t = sum(l.map { |x| x[0] }) | |
m += 1 if (t > t2) | |
t2 = t | |
w = l.select { |x| x[0] >= n1 }.size | |
puts([y, t, l.size, m.to_f / (y + 1), w.to_f / l.size, c].join("\t")) | |
y += 1 | |
end while (!l.empty?) | |
puts | |
end | |
fs().each_with_index { |f, i| smooth(f, i) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment