Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def f1(n)
- n = (n * 3 + 1) / 2 while (n.odd?)
- n /= 2 while (n.even?)
- return n
- end
- def f2(n)
- return n.odd? ? (n * 3 + 1) / 2 : n / 2
- end
- def f3(n)
- return n.odd? ? (n * 3 + 1) : n / 2
- end
- def d(s)
- c = s.split('').select { |x| x == '1' }.size
- d = c.to_f / s.length
- return d
- end
- def read(fn)
- l = (f = File.open(fn)).readlines.map { |x| Kernel.eval(x) }
- f.close
- $stderr.puts("#{fn} #{l.size}")
- return l
- end
- def dist_j(r = nil)
- l = $db.nil? ? ($db = read($fndb)) : $db
- z1 = rand(l.size)
- ns = l[z1]['n'].to_s(2)
- r = rand(ns.length) if (r.nil?)
- ns[r, 1] = (ns[r, 1].to_i ^ 1).to_s
- n = ns.to_i(2)
- return n
- end
- def disordered(n)
- n1 = n
- l = [n]
- l1 = []
- i = nil
- while (n >= n1 && n != 1)
- n = $f.call(n)
- l << n
- next if (!i.nil?)
- l1 << d(n.to_s(2)) - 0.5
- l1.shift if (l1.size == 3)
- i = l.size - 1 if (i.nil? && l1.size == 2 && l1[0] * l1[1] < 0)
- end
- cm = (0...l.size).max_by { |x| l[x] }
- return if (i.nil?)
- return {'n2' => l[i], 'i' => i}
- end
- def advc(n)
- n1 = n
- l = [n]
- cg = nil
- while (n != 1)
- n = $f.call(n)
- l << n
- cg = l.size - 1 if (cg.nil? && n < n1)
- end
- cg = l.size - 1 if (cg.nil?)
- cma = (0..cg).max_by { |x| l[x] }
- return l[0..cma]
- end
- def n1()
- loop \
- {
- n = dist_j()
- x = disordered(n)
- next if (x.nil?)
- n = x['n2']
- n /= 2 while (n.even?)
- return n
- }
- end
- def run()
- $fndb = 'mix19/mixdb.txt'
- f = File.open('gnuplot.cmd', 'w')
- f.puts('set y2tics; set ytics nomirror; unset key; set colors classic; plot \\')
- f.puts("'-' using 1:2:3 with line lw 6 linecolor palette axes x2y2,\\")
- 1.times { |x| f.puts("'-' using 1:2:3 title 'fn#{x + 1}' with line lw 1 linecolor palette,\\") }
- f.puts
- tf = 1
- $f = [lambda { |x| f1(x) }, lambda { |x| f2(x) }, lambda { |x| f3(x) }][tf]
- l = (1..1000).map { advc(n1()) }.uniq
- $stderr.puts({'ls' => l.size}.inspect)
- l.sort_by! { |x| x.size }
- n = 200
- r = -n..-1
- l[r].reverse.each_with_index { |x, i| f.puts([i, x.size, n - i].join("\t")) }
- f.puts('eof')
- l[r].reverse.each_with_index \
- {
- |l1, c|
- w0 = l1[0].to_s(2).length
- l1.each_with_index \
- {
- |x, j|
- wr2 = x.to_s(2).length.to_f / w0
- wr2b = Math.log(x) / Math.log(l1[0])
- f.puts([j.to_f, wr2b, n - c].join("\t"))
- }
- f.puts
- }
- f.puts('eof')
- f.close
- end
- run()
Add Comment
Please, Sign In to add comment