Guest User

Untitled

a guest
May 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. def f1(n)
  2. n = (n * 3 + 1) / 2 while (n.odd?)
  3. n /= 2 while (n.even?)
  4. return n
  5. end
  6.  
  7. def f2(n)
  8. return n.odd? ? (n * 3 + 1) / 2 : n / 2
  9.  
  10. end
  11.  
  12. def avg(l)
  13. return 0 if (l.empty?)
  14. t = 0
  15. l.each { |x| t += x }
  16. return t.to_f / l.size
  17. end
  18.  
  19. def total(l)
  20. t = 0
  21. l.each { |x| t += x }
  22. return t
  23. end
  24.  
  25. def h(n, l0, l1)
  26. ns = n.to_s(2)
  27. nl = ns.length
  28. l1.concat(ns.split(/0+/).map { |x| x.length.to_f / nl })
  29. l0.concat([ns.split(/1+/)[1..-1]].compact.flatten.map { |x| x.length.to_f / nl })
  30.  
  31. end
  32.  
  33.  
  34. def count(x)
  35.  
  36.  
  37. n = n1 = x['n']
  38. l = [n]
  39. while (n >= n1 && n != 1)
  40. n = {'packed' => true, 'unpacked' => false}[$w] ? f1(n) : f2(n)
  41. l << n
  42. end
  43. cm = (0...l.size).max_by { |x| l[x] }
  44.  
  45. l0 = []
  46. l1 = []
  47.  
  48. l[0..cm].each \
  49. {
  50. |n|
  51. h(n, l0, l1)
  52. }
  53. return l0, l1
  54. end
  55.  
  56. def uniq(l)
  57.  
  58. a = {}
  59. l.each { |x| a[x['n']] = x }
  60. $stderr.puts({'ls1' => l.size, 'ls2' => a.size}.inspect)
  61. return a.values
  62.  
  63. end
  64.  
  65.  
  66. # $w = 'packed'
  67. $w = 'unpacked'
  68.  
  69. l = uniq(File.open('tmp/mixdb.txt').readlines.map { |x| Kernel.eval(x) })
  70.  
  71. j = 0
  72. at = {}
  73. l = l.sort_by { |x| x['cm'] }.reverse
  74.  
  75.  
  76. c = 500
  77.  
  78. c.times \
  79. {
  80. |i|
  81.  
  82. j = (i.to_f / (c - 1) * (l.size - 1)).to_i
  83. l0, l1 = count(l[j])
  84.  
  85. l1.sort!
  86. l1 = l1.select { |x| x >= 1.0 / 8 }
  87.  
  88. l1.each_with_index \
  89. {
  90. |y, x|
  91. puts([x.to_f / (l1.size - 1), y, c - i].join("\t"))
  92. }
  93. puts
  94. }
Add Comment
Please, Sign In to add comment