Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created November 17, 2017 02:19
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/2e8b58ced14435a96c8f304498a5a6e6 to your computer and use it in GitHub Desktop.
Save vznvzn/2e8b58ced14435a96c8f304498a5a6e6 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 stat(l)
l = [0] if (l.empty?)
t = t2 = 0
l.each \
{
|x|
t += x
t2 += x ** 2
}
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
return a, sd, l.max.to_f, l.min.to_f
end
def stat2(l, t, n)
return Hash[[["a#{n}", "sd#{n}", "mx#{n}"], stat(l)[0..2].map { |x| x / t }].transpose]
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def data(n)
ns = n.to_s(2)
nl = ns.length
m = nl / 2
nsh = ns[0..m]
nsl = ns[m..-1]
asdm1 = stat2(ns.split(/0+/).map { |x| x.length }, nl, 1)
l1 = ns.split(/1+/)
l1.shift
asdm0 = stat2(l1.map { |x| x.length }, nl, 0)
return {'n' => n, 'ns' => ns, 'nl' => nl, 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1).merge(asdm0)
end
def count(n)
c = 0
n1 = n
l = [n]
begin
n = f2(n)
l << n
c += 1
end while n >= n1
return {'c' => c, 'l' => l}
end
def dist(c)
return (0...c).map { |i| count(dense(200, i.to_f / (c - 1))) }
end
def out(fn, a)
f = File.open(fn, 'a')
f.puts(a.keys.join("\t")) if (f.size == 0)
f.puts(a.values.join("\t"))
f.close
end
l = dist(500)
l.sort_by! { |x| -x['c'] }
File.open(fn = 'out.txt', 'w').close
l[0..20].each_with_index \
{
|x1, i|
x1['l'].each_with_index \
{
|x, j|
out(fn, data(x).merge({'i' => i, 'j' => j}))
}
out(fn, {})
}
f = File.open('gnuplot.cmd', 'w')
['nl', 'a1', 'sd1', 'mx1'].each \
{
|x|
f.puts("plot '#{fn}' using (column('j')):(log(column('#{x}'))):(column('i')) with line linecolor palette title 'log #{x}'")
f.puts("pause -1")
}
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment