Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created December 15, 2018 02:15
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/77c9f8cd53342495f3cae683e48684fe to your computer and use it in GitHub Desktop.
Save vznvzn/77c9f8cd53342495f3cae683e48684fe to your computer and use it in GitHub Desktop.
def f1(n)
return n.odd? ? n * 3 + 1 : n / 2
end
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def f3(n)
n /= 2 while (n.even?)
n = (n * 3 + 1) / 2 while (n.odd?)
return n
end
def e(ns)
l1 = ns.split(/0+/)
l0 = ns.split(/1+/)
l0 = [''] if (l0.nil?)
return (l0 + l1).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
def len01(ns)
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 cut(c, n, b)
s = b.to_s * c
a = (1...c).to_a
l = (1..n).map { a.delete_at(rand(a.size)) }
l.sort.reverse.each { |x| s[x, 0] = '-' }
return s.split('-')
end
def blend(w, r)
r1 = [[1, r].max, w - 1].min
r0 = w - r1
c = [r0, r1].min - 1
return (0..c).map \
{
|x|
l0 = cut(r0, x, 0)
l1 = cut(r1, x, 1)
[l1, l0].transpose.join
}
end
def wd(ns)
n = ns.to_i(2)
n2 = f3(n)
ns2 = n2.to_s(2)
return ns2.length.to_f / ns.length, Math.log(f2(n)) / Math.log(n)
end
c = 100
f = File.open('gnuplot.cmd', 'w')
f.puts("$dat << eof")
f.puts("d\te\twd1\twd2")
l = []
(0..c).each \
{
|x|
l1 = blend(c, x)
l1.each { |x| f.puts([d(x), e(x), wd(x)].join("\t")) }
f.puts
l += l1
}
f.puts("eof")
f.puts("plot $dat using (column('d')):(column('e')):(column('wd1')) with points lt palette pt 5 ps 0.75 title 'd:e:wd'")
f.puts("pause -1")
f.puts("plot $dat using (column('d')):(column('e')):(column('wd2')) with points lt palette pt 5 ps 0.75 title 'd:e:wd2'")
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment