Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
end
def seq(n)
n1 = n
l = []
begin
l << n
n = f2(n)
end while (n != 1)
return l
end
def count(ns)
l = seq(ns.to_i(2))
cm1 = (0...l.size).max_by { |x| l[x] }
cg = l.index { |x| x < l[0] }
cm = (0...cg).max_by { |x| l[x] }
cg1 = l.rindex { |x| x > l[0] }
return {'ns' => ns,
'cm' => cm,
'cm1' => cm1,
'cg' => cg,
'cg1' => cg1,
'l' => l
}
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.shift if (l[0] == '')
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 sample(l, c)
return l if (l.size < c)
r = l.size / c
l2 = *(0...l.size).select { |x| x % r == 0 }
l2[c..-1] = []
return l.values_at(*l2)
end
def graph(f, l, t = '')
f.puts("$dat << eof")
l.each \
{
|l1, c|
l1.each { |x| f.puts([x, c].join("\t")) }
f.puts
}
f.puts("eof")
t2 = t.gsub(' ', '')
w1 = "set terminal gif; set output 'out_#{t2}.gif';";
w2 = "unset terminal; unset output;";
# w2 = "pause -1;"
f.puts("print '#{t}'; set title '#{t}'; #{w1} plot [][0:1] $dat using 1:2:3 with line linecolor palette lw 2; #{w2}")
$i = 0 if ($i.nil?)
$i += 1
$stderr.puts(t)
end
def line(l, f, w = false)
a = {}
l.each \
{
|i, ns|
next if (w && a.member?(ns.length))
a[ns.length] = f.call(ns)
}
return a.keys.sort.map { |x| [x, a[x]] }
end
l = (1..100).map { count(dense(50, 0.5))['l'] }
.sort_by { |x| x.size }
.map { |l1| [(0...l1.size).to_a, l1.map { |x| x.to_s(2) }].transpose }
f = File.open('gnuplot.cmd', 'w')
graph(f, l.map { |l1| [l1.map { |x| [x[0], d(x[1])] }, l1.size] }, 'd')
graph(f, l.map { |l1| [l1.map { |x| [x[0], e(x[1])] }, l1.size] }, 'e')
graph(f, l.map { |l1| [l1.map { |x| [-(l1.size - x[0]), d(x[1])] }, l1.size] }, 'd2')
graph(f, l.map { |l1| [l1.map { |x| [-(l1.size - x[0]), e(x[1])] }, l1.size] }, 'e2')
graph(f, l.map { |l1| [line(l1, lambda { |x| d(x) }), l1.size] }, 'd by w')
graph(f, l.map { |l1| [line(l1, lambda { |x| d(x) }, true), l1.size] }, 'd by w 2')
graph(f, l.map { |l1| [line(l1, lambda { |x| e(x) }), l1.size] }, 'e by w')
graph(f, l.map { |l1| [line(l1, lambda { |x| e(x) }, true), l1.size] }, 'e by w 2')
f.close
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.