Guest User

Untitled

a guest
Mar 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. def f2(n)
  2. return n.odd? ? (n * 3 + 1) / 2 : n / 2
  3.  
  4. end
  5.  
  6. def f3(n)
  7. n /= 2 while (n.even?)
  8. n = (n * 3 + 1) / 2 while (n.odd?)
  9. return n
  10.  
  11. end
  12.  
  13. def f4(n)
  14. n /= 2 while (n.even?)
  15. n = (n * 3 + 1) / 2 if (n.odd?)
  16. end
  17.  
  18. def climb(n)
  19.  
  20. n1 = n
  21. l = [n]
  22. begin
  23. n = f2(n)
  24. l << n
  25. end while (n > 2)
  26.  
  27. m = (0...l.size).max_by { |x| l[x] }
  28.  
  29. b = m
  30. b += 1 while (l[b] > n1)
  31.  
  32. l = l.map { |x| x.to_s(2).reverse }
  33. return l[0..m], l[m..b], l[(b + 1)..-1]
  34.  
  35.  
  36. end
  37.  
  38. def graph1(f, l, m, j)
  39.  
  40. w = l.max_by { |x| x.length }.length
  41.  
  42. f.puts("set colors classic; \\")
  43. f.puts("set title '##{m} #{['climb', 'fall', 'tail'][j]}'; plot \\")
  44. f.puts("[0:#{w}][0:#{l.size - 1}] '-' matrix with image title ''")
  45.  
  46. l.each_with_index \
  47. {
  48. |z, i|
  49.  
  50. c = w - z.length
  51. z[z.length...w] = '0' * c
  52.  
  53. f.puts(z.split('').join(' '))
  54. }
  55. f.puts("eof")
  56. f.puts("eof")
  57. f.puts("pause -1;")
  58.  
  59. end
  60.  
  61. def graph(l)
  62.  
  63.  
  64. f = File.open(fn = "gnuplot.cmd", 'w')
  65. f.puts("set palette negative grayscale; unset colorbox;")
  66.  
  67. l.each_with_index \
  68. {
  69. |x, i|
  70. climb(x).each_with_index { |x, j| graph1(f, x, i, j) }
  71. }
  72.  
  73. f.close
  74. $stderr.puts(fn)
  75.  
  76. end
  77.  
  78. def db()
  79.  
  80. l = File.open('db.txt').readlines.map { |x| Kernel.eval(x) }
  81.  
  82. $stderr.puts("ls1 #{l.size}")
  83. l = l.select { |x| x['cg'] > 50 }.sort_by { |x| -x['cg'] }[0...10]
  84.  
  85. $stderr.puts("ls2 #{l.size}")
  86.  
  87. return l.map { |x| x['n'] }
  88.  
  89. end
  90.  
  91. if (File.exists?(fn = "grid.txt")) then
  92. l = File.open(fn).readlines.map { |x| x.to_i }
  93. $stderr.puts("#{fn} #{l.size}")
  94. else
  95. l = db()
  96. end
  97.  
  98.  
  99. graph(l)
Add Comment
Please, Sign In to add comment