Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created December 26, 2018 03:27
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/fc07bc38300251e57800dd7174b43468 to your computer and use it in GitHub Desktop.
Save vznvzn/fc07bc38300251e57800dd7174b43468 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def count(n, l = [])
c = 0
n1 = n
while (n > 1)
n = f2(n)
l << n.to_s(2).length
c += 1
end
return c
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)
end
def len01(ns)
nl = ns.length
l1 = ns.split(/0+/).map { |x| x.length }
l2 = ns.split(/1+/)[1..-1]
l2 = [''] if (l2.nil?)
l0 = l2.map { |x| x.length }
return l0, l1
end
def mx01(ns)
return len01(ns).flatten.max.to_f / ns.length
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
l = []
w = 100
100.times \
{
ns = dense(w, 0.5)
n = ns.to_i(2)
c = count(n, l1 = [])
l << {'ns' => ns, 'l1' => l1}
}
l.sort_by! { |x| x['l1'].size }
f = File.open('out.txt', 'w')
f.puts(['c', 'r'].join("\t"))
c1 = l[0]['l1'].size - 1
$stderr.puts(c1)
l.each_with_index \
{
|x, j|
f.puts([x['l1'].size, (x['l1'][c1] - w).to_f / c1].join("\t"))
}
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment