Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created December 2, 2017 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/1c5836f7016338c812f39389a9e955d7 to your computer and use it in GitHub Desktop.
Save vznvzn/1c5836f7016338c812f39389a9e955d7 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s).to_i(2)
end
def count(n)
c = 0
n1 = n
l = [n]
c2 = nil
begin
n = f2(n)
l << n
c += 1
c2 = c if (c2.nil? && n <= n1)
end while n != 1
j = (0...l.size).max_by { |x| l[x] }
return {'c' => c, 'c2' => c2, 'n' => n1, 'j' => j}
end
def dist(c)
return (0...c).map { |i| count(dense(100, i.to_f / (c - 1))) }
end
def sample(n)
s = {}
n.times \
{
l = dist(500)
l.each { |x| s[x['n']] = x }
}
l2 = s.values
l1 = l2.map { |x| x['c'] }
mn = l1.min
mx = l1.max * 0.75
$stderr.puts([l2.size, mn, mx].inspect)
l2.each { |x| x['z'] = (x['c'].to_f - mn) / (mx - mn) }
l = []
(0..100).each \
{
|i|
d = 1.0 - i.to_f / 100
# d = i.to_f / 100
j = (0...l2.size).min_by { |x| (l2[x]['z'] - d).abs }
l << l2.delete_at(j)
}
# return l
return l.sort_by { |x| x['c'] }
end
def out(fn, l)
f = File.open(fn, 'w')
f.puts(l[0].keys.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.close
end
out('out.txt', sample(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment