Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. def f2(n)
  2. return n.odd? ? (n * 3 + 1) / 2 : n / 2
  3.  
  4. end
  5.  
  6. def dense(w, d)
  7. w2 = w - 1
  8.  
  9. a = (0...w2).to_a
  10. s = '0' * w2
  11. (1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
  12. return ('1' + s)
  13.  
  14. end
  15.  
  16. def lenz(ns, p)
  17. l = ns.split(p)
  18. l = [''] if (l.nil? || l.empty?)
  19. return l.map { |x| x.length }
  20. end
  21.  
  22. def len01z(ns)
  23. return len(ns, /0+/), len(ns, /1+/)
  24. end
  25.  
  26. def mx01z(ns)
  27. l1, l0 = len01(ns)
  28. return l1.max, l0.max
  29. end
  30.  
  31. def lenx(ns, p)
  32. l = ns.split(p)
  33. l = [] if (l.nil?)
  34. l.shift if (l[0] == '')
  35. return l.map { |x| x.length }
  36. end
  37.  
  38. def len01x(ns)
  39. return lenx(ns, /0+/), lenx(ns, /1+/)
  40. end
  41.  
  42. def mx01x(ns)
  43. l1, l0 = len01x(ns)
  44. return (l1 + l0).max
  45. end
  46.  
  47. def add(l, mxp, n, x)
  48.  
  49. p = (n % 2).to_s + x['p']
  50.  
  51. # l1m, l0m = mx01(p)
  52. # return if ([l0m, l1m].max >= mxp)
  53.  
  54. return if (mx01x(p) >= mxp)
  55.  
  56.  
  57. w = n.to_s(2).length
  58.  
  59. l1 = [n] + x['l']
  60.  
  61. c = x['c'] + (n.even? ? 1 : 0)
  62. l << {'n' => n,
  63. 'p' => p,
  64. 'w' => n.to_s(2).length,
  65. 'r' => c.to_f / w,
  66. 'l' => l1,
  67. 'c' => c}
  68. end
  69.  
  70. def back(ns)
  71.  
  72. mxp = 4
  73. add(l = [], mxp, ns.to_i(2), {'p' => '', 'l' => [], 'c' => 0})
  74.  
  75. mx = nil
  76. k = 'r'
  77. t = 0
  78. e = 2
  79.  
  80. loop \
  81. {
  82. return if (l.empty?)
  83. break if (!mx.nil? && mx[k] >= e)
  84.  
  85. l.sort_by! { |x| -x[k] }
  86. mx = [l[0], mx].compact.max_by { |x| x[k] }
  87.  
  88. x = l.delete_at(rand([l.size, 50].min))
  89.  
  90. puts([t, x[k]].join("\t")) if (t % 100 == 0)
  91. n = x['n']
  92.  
  93. add(l, mxp, (n - 1) / 3, x) if ((n - 1) % 3 == 0 && ((n - 1) / 3).odd?)
  94. add(l, mxp, n * 2, x)
  95.  
  96. t += 1
  97. }
  98. return mx
  99.  
  100. end
  101.  
  102. def reverse(w, mk)
  103. t = 0
  104. x = nil
  105. loop \
  106. {
  107. t += 1
  108. ns = mk.call(w)
  109. x = back(ns)
  110. break if (!x.nil?)
  111. }
  112. # $stderr.puts([t, x].inspect)
  113. return x
  114. end
  115.  
  116. def d(s)
  117. c = s.split('').select { |x| x == '1' }.size
  118. d = c.to_f / s.length
  119. return d
  120.  
  121. end
  122.  
  123. def e(ns)
  124. return len01x(ns).flatten.size.to_f / ns.length
  125. end
  126.  
  127. def fmt1(l, f)
  128.  
  129. w = l.max_by { |x| x.length }.length
  130.  
  131. f.puts("set colors classic; set ytics nomirror; set y2tics; set key top right opaque;\\")
  132. f.puts("plot \\")
  133. f.puts("[0:#{l.size - 1}][0:#{w}] '-' matrix using 2:1:3 with image title '', \\")
  134. f.puts("'-' using 1 with line title 'e' axes x1y2 lw 3, \\")
  135. f.puts("'-' using (column('d')) with line axes x1y2 lw 3 ")
  136. l1 = []
  137. l.each_with_index \
  138. {
  139. |z, i|
  140.  
  141. l1 << [d(z), e(z)]
  142. c = w - z.length
  143. z[z.length...w] = '0' * c
  144.  
  145. f.puts(z.split('').join(' '))
  146. }
  147. f.puts("eof")
  148. f.puts("eof")
  149. l1.each { |x| f.puts(x[1]) }
  150. f.puts("eof")
  151. f.puts("d")
  152. l1.each { |x| f.puts(x[0]) }
  153. f.puts("eof")
  154.  
  155.  
  156. end
  157.  
  158. def grid(l, b)
  159.  
  160. f = File.open(fn = "gnuplot#{b}.cmd", 'w')
  161. f.puts("set palette negative grayscale; unset colorbox;")
  162.  
  163. fmt1(l.map { |x| x.to_s(2).reverse }, f)
  164. f.close
  165.  
  166. end
  167.  
  168. def adj(x, m, n, s2, p2, p, x3)
  169. s2.replace(p[0...x] + (x3 % 2).to_s)
  170. m = (s2[x, 1] == p[x, 1])
  171. n = p2.reverse.to_i(2)
  172. p2[x, 1] = m ? '11' : '01'
  173. return m, n, x3
  174. end
  175.  
  176. def terras121(p)
  177.  
  178. p2 = ['01', '11'][p[0, 1].to_i]
  179. n = 1
  180. n3 = 0
  181. m = p2 == '11'
  182. x3 = (p2.reverse.to_i(2)) >> 1
  183.  
  184. (1...p.length).each \
  185. {
  186. |x|
  187.  
  188. if (!m)
  189. ns = n.to_s(2)
  190. ns[0, 1] = ''
  191. m1 = x3 - 3**n3
  192. n1 = f2(m1)
  193. end
  194.  
  195. n3 = p[0...x].split('').select { |z| z == '1' }.size
  196.  
  197. s2 = ''
  198. m, n, x3 = adj(x, m, n, s2, p2, p, m ? (f2(x3) + 3**n3) : (n1 + 3**n3))
  199. }
  200.  
  201. n = p2.reverse.to_i(2)
  202.  
  203. return n
  204. end
  205.  
  206. def seq(n)
  207. l = [n]
  208. while (n != 1)
  209. n = f2(n)
  210. l << n
  211. end
  212. return l
  213.  
  214. end
  215.  
  216. def construct(b)
  217.  
  218. x = nil
  219. begin
  220. ns = dense(100, 0.5)
  221. ns[49, 50] = b * 50
  222. ns2 = terras121(ns).to_s(2)
  223.  
  224. x = back(ns2)
  225.  
  226. end while (x.nil?)
  227.  
  228.  
  229. x.delete('l')
  230. p(x)
  231. l = seq(x['n'])
  232.  
  233. return l
  234. end
  235.  
  236. ['0', '1'].each { |x| grid(construct(x), x) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement