Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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).to_i(2)
- end
- def stat(l)
- l = [0] if (l.empty?)
- t = t2 = 0
- l.each \
- {
- |x|
- t += x
- t2 += x ** 2
- }
- c = l.size
- a = t.to_f / c
- z = t2.to_f / c - a ** 2
- sd = Math.sqrt(z < 0 ? 0 : z)
- return a, sd, l.max.to_f, l.min.to_f
- end
- def stat2(l, t, n)
- return Hash[[["a#{n}", "sd#{n}", "mx#{n}"], stat(l)[0..2].map { |x| x / t }].transpose]
- end
- def d(s)
- c = s.split('').select { |x| x == '1' }.size
- d = c.to_f / s.length
- return d
- end
- def data(n)
- ns = n.to_s(2)
- nl = ns.length
- m = nl / 2
- nsh = ns[0..m]
- nsl = ns[m..-1]
- asdm1 = stat2(ns.split(/0+/).map { |x| x.length }, nl, 1)
- l1 = ns.split(/1+/)
- l1.shift
- asdm0 = stat2(l1.map { |x| x.length }, nl, 0)
- return {'n' => n, 'ns' => ns, 'nl' => nl, 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1).merge(asdm0)
- end
- def sum(l)
- t = 0.0
- l.each { |x| t += x }
- return t
- end
- def count(n)
- n1 = n
- l = [n]
- begin
- n = f2(n)
- l << n
- end while n > n1
- m = (0...l.size).max_by { |x| l[x] }
- r = l[m].to_s(2).length - n1.to_s(2).length
- return {'c' => l.size, 'n' => n1, 'l' => l, 'm' => m, 'r' => r}
- end
- def load(s)
- raise if (s.nil?)
- d = "out/#{s}"
- l = Dir.glob("#{d}/*").select { |x| x =~ /out\d+.txt$/}
- n = l.size
- i = n - 1
- a = Kernel.eval(File.open("#{d}/out#{i}.txt").readlines[0])
- l = a['lb'].map { |x| x.size }
- raise if (sum(l) != 0)
- return a
- end
- def dot(x, z)
- t = z['c']
- (z.keys - ['c']).each { |v| t += z[v] * x[v] }
- return t
- end
- def detect(n, a)
- $seen = {} if ($seen.nil?)
- if $seen.member?(n) then
- $seen['h'] = $seen.fetch('h', 0) + 1
- return $seen[n]
- end
- x1 = data(n)
- i = 0
- w = 1.0
- x1['wr'] = 1.0
- loop \
- {
- x2 = {}
- (a['v'] + ['wr']).each \
- {
- |vy|
- x2[vy] = dot(x1, a[vy].select { |k, x| (a['v'] + ['c']).member?(k) })
- }
- x1 = x2
- w *= x1['wr']
- i += 1
- break if (w < 1.0 || i == 500)
- }
- $seen[n] = i
- return i
- end
- def out(fn, l)
- f = File.open(fn, 'w')
- f.puts(l[0].keys.join("\t"))
- l.each { |x| f.puts(x.values.join("\t")) }
- f.close
- f = File.open('gnuplot.cmd', 'w')
- f.print('set colors classic; set ytics nomirror; set y2tics; plot [][0:] ')
- l[0].keys.each_with_index \
- {
- |x, i|
- f.puts("'#{fn}' using (column('#{x}')) with line axes x1y#{i + 1},\\")
- }
- f.puts
- f.close
- end
- def dist(c)
- l = []
- seen = {}
- 3.times {
- l += (0...c).map \
- {
- |i|
- n = dense(100, i.to_f / (c - 1))
- next if (seen.member?(n))
- seen[n] = nil
- count(n)
- }.compact
- }
- l.sort_by! { |x| x['m'] }
- l.reverse!
- return l
- end
- def mono(l1)
- l = l1.map { |x| x['a'] }
- c = 0
- m = l[0]
- mx = nil
- j = 0
- l2 = []
- (1...l.size).each \
- {
- |i|
- c += 1 if (l[i] < l[i - 1])
- if (l[i] < m) then
- l2 << i - j
- m, j = [l[i], i]
- end
- mx = [mx, i - j].compact.max
- }
- mx = 0 if (mx.nil?)
- return {'mx' => mx, 'ls' => l.size, 'l' => l1}
- end
- def avg(l2, x1)
- m = 20
- c = 0
- t = 0
- l = []
- l1 = []
- (x1...l2.size).each \
- {
- |x|
- l << l2[x]['d']
- t += l[-1]
- t -= l.shift if (l.size > m)
- if (l.size == m) then
- a = t.to_f / l.size
- l2[x]['a'] = a
- l1 << {'a' => a, 'w' => l2[x]['w'] }
- end
- }
- return mono(l1)
- end
- def test(a, f, l1)
- l2 = []
- l = dist(500)
- i = 0
- l.select { |x| x['m'] >= 50}.each_with_index \
- {
- |y, j|
- # p([j + 1, y['l'][0]])
- x1 = l2.size
- y['l'][0..y['m']].each \
- {
- |x|
- l2 << {'i' => i, 'w' => x.to_s(2).length, 'd' => detect(x, a)}
- i += 1
- }
- l1 << avg(l2, x1)
- f.puts(l1[-1].values.join("\t"))
- f.flush
- l2 << {}
- i += 20
- }
- l2[0]['a'] = nil
- # out('out.txt', l2)
- $stderr.puts("hits #{$seen['h']}") if ($seen.member?('h'))
- end
- a = load(ARGV[0])
- l = []
- f = File.open('out.txt', 'w')
- 10.times { test(a, f, l) }
- f.close
- z = l.max_by { |x| x['mx'] }
- $stderr.puts(['mx', z['mx']].inspect)
- out('out2.txt', z['l'])
Add Comment
Please, Sign In to add comment