Skip to content

Instantly share code, notes, and snippets.

@vznvzn vznvzn/bitwise24.rb
Created Feb 10, 2019

Embed
What would you like to do?
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def len(ns, p)
l = ns.split(p)
l = [''] if (l.nil? || l.empty?)
return l.map { |x| x.length }
end
def len01(ns)
return len(ns, /0+/), len(ns, /1+/)
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def mx01(ns)
return len01(ns).flatten.max
end
def limbo(l)
mxda = mxea = mxa2 = 0
l.each \
{
|x|
ns = x.to_s(2)
d = d(ns)
da = (d - 0.5).abs
l1, l0 = len01(ns)
e = (l1 + l0).size.to_f / ns.length
ea = (e - 0.5).abs
a1 = l1.inject { |t, y| t + y }.to_f / l1.size
a2 = (a1 - 2.0).abs
mxda = [mxda, da].max
mxea = [mxea, ea].max
mxa2 = [mxa2, a2].max
}
return {'mxda' => mxda, 'mxea' => mxea, 'mxa2' => mxa2 * 100}
end
def count(x, l2 = [], l = [])
n = n1 = x['n']
l << n1
while (n != 1)
n = f2(n)
l << n
end
d = d(ns = n1.to_s(2))
cg = l.index { |x| x < n1 }
cg = 0 if (cg.nil?)
cm = (0..cg).max_by { |x| l[x] }
nl = ns.length
c = l.size
l1 = l[nl..cm]
l1 = [] if (l1.nil?)
l2.replace((nl...c).map { |x| {'x' => Math.log(l[x])} })
l[(cg + 1)..-1] = []
return x.merge({
'd' => d,
'e' => e(ns),
'da' => (d - 0.5).abs,
'ns' => ns,
'cm' => cm,
'cg' => cg,
'c' => c,
'nl' => nl,
'cmnl' => cm - nl,
}.merge(limbo(l1)))
end
def out(a, a2 = {}, t = nil)
fn = 'out.txt'
k = a.keys
if ($f1.nil?) then
$f1 = File.open(fn, 'w')
$f1.puts(k.join("\t"))
f2 = File.open('gnuplot.cmd', 'w')
st = t.nil? ? 'unset title; ' : "set title '#{t}'; "
f2.puts("set ytics nomirror; set y2tics; set colors classic; set key top left box opaque; #{st}; plot \\")
(a.keys - ['t']).each \
{
|x|
f2.puts("'#{fn}' using (column('t')):(column('#{x}')) with line title '#{x}' #{a2[x]}, \\")
}
f2.puts
f2.close
end
$f1.puts(a.values_at(*k).join("\t"))
$f1.flush
end
def next2(z, l, ks, t)
out(z.select { |k, v| !['n', 'ns', 'p'].member?(k) },
fmt(ks), t.inspect) if (z['t'] % 10 == 0)
p = z['p'] + 1
l << [z, count({'n'=>z['n'] + 2 ** p, 'p'=>p, 't' => z['t']}), z.merge({'p'=>p})]
return
end
def maxs(l)
l.map { |x| x[-1, 1] == '-' ? x.dup.chop : x }
end
def hard(ks)
$stderr.puts(ks.inspect)
$f1 = nil
l = []
mx = maxs(ks)
next2(count({'n' => 1, 'p' => 0, 't' => 0}), l, mx, ks)
add = nil
seen = {}
f = File.open($fn, 'w')
c = 20
5e3.to_i.times \
{
|t|
l2 = (0...l.size).map { {} }
mx.each \
{
|k2|
l1 = (0...l.size).map { |x| {k2 => l[x][1][k2], 'i' => x} }
l1.sort_by! { |x| x[k2] }
l1 = l1.reverse if (ks.member?(k2))
(0...l1.size).each { |x| l2[l1[x]['i']][k2] = x }
}
l2.each { |x| x['z'] = x.values.inject(0) { |t, x| t + x } }
l1 = (0...l2.size).sort_by { |x| l2[x]['z'] }
a = {'t1' => t, 'l1' => l.size, 'cs' => seen.size}.merge(l[l1[0]][1])
if (!seen.member?(a['n'])) then
seen[a['n']] = nil
f.puts(a)
f.flush
end
$stderr.puts(a.select { |k, | !['n'].member?(k)}) if ((t + 1) % 500 == 0)
i = l1[rand([l1.size, c].min)]
z = l.delete_at(i)
[z[1], z[2]].each { |x| next2(x.merge({'t' => t}), l, mx, ks) }
l.delete_at(l1[-1]) if (l.size > 2000)
}
f.close
end
def fmt(ks = [])
fmt = {}
ks.each { |x| fmt[x] = 'lw 2' }
['da', 'd', 'e', 'mxda', 'mxea'].each { |x| fmt[x] = fmt.fetch(x, '') + ' dt 3 axes x1y2' }
return fmt
end
ks = ['mxda', 'mxea', 'mxa2']
ks += ['nl', 'cm', 'cg', 'c', 'cmnl']
$fn = 'mixdb.txt'
hard(ks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.