Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created January 12, 2019 19:41
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/534d6c6bb0cb077f3e4f49a7906eff12 to your computer and use it in GitHub Desktop.
Save vznvzn/534d6c6bb0cb077f3e4f49a7906eff12 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def count(ns)
n = ns.to_i(2)
n1 = n
l = [n1]
while (n > 1)
n = f2(n)
l << n
end
cg = l.index { |x| x < n1 }
cm = (0..cg).max_by { |x| l[x] }
cg1 = l.rindex { |x| x >= n1 }
cm1 = (0...l.size).max_by { |x| l[x] }
return {'cm' => cm,
'cg' => cg,
'cm1' => cm,
'cg1' => cg,
'c' => l.size,
'ncm' => Math.log(l[cm]),
'ncm1' => Math.log(l[cm1]),
'ncg' => Math.log(l[cg]),
'ncg1' => Math.log(l[cg1]),
'e' => e(ns),
'nb' => Math.log(l[ns.length]),
'ns' => ns,
'l' => l
}
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 d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def fmt(l, f)
w = l.max_by { |x| x.length }.length
f.puts("set colors classic; \\")
f.puts("plot \\")
f.puts("[0:#{w}][0:#{l.size - 1}] '-' matrix with image title ''")
l.each_with_index \
{
|z, i|
c = w - z.length
z[z.length...w] = '0' * c
f.puts(z.split('').join(' '))
}
f.puts("eof")
f.puts("eof")
# f.puts("pause -1;")
end
def grid(l)
f = File.open(fn = "gnuplot1.cmd", 'w')
f.puts("set palette negative grayscale; unset colorbox;")
fmt(l, f)
f.close
end
def remove(a, ks)
a = a.dup
ks.each { |x| a.delete(x) }
return a
end
def out(l, k)
f2 = File.open("out_#{k}.txt", 'w')
l.each_with_index \
{
|x, c|
x['l'].each_with_index \
{
|y, j|
f2.puts([j, y.to_s(2).length, c].join("\t"))
}
f2.puts
}
f2.close
end
w = 100
l = (1..100).map { count(dense(w, 0.5)) }
grid(l.sort_by { |x| x['cg'] }.map { |x| x['ns'].reverse })
ks = remove(l[0], ksx = ['l', 'ns']).keys
$stderr.puts(ks.inspect)
f = File.open('out.txt', 'w')
f.puts(ks.join("\t"))
l.each { |x| f.puts(remove(x, ksx).values.join("\t")) }
f.close
ks.each { |k| out(l.sort_by { |x| x[k] }, k) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment