Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created December 31, 2018 21:04
Show Gist options
  • Save vznvzn/0b1c98d342dfa8e2d1ba58bad62003e3 to your computer and use it in GitHub Desktop.
Save vznvzn/0b1c98d342dfa8e2d1ba58bad62003e3 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def count(ns)
n1 = n = ns.to_i(2)
l1 = [n]
while (n >= n1 && n != 1)
n = f2(n)
l1 << n
end
nl = ns.length
cm = (0...l1.size).max_by { |x| l1[x] }
return {'ns' => ns,
'nl' => nl,
'cm' => cm,
'cmnl' => cm - nl}
end
l = [count('1')]
f = File.open('gnuplot.cmd', 'w')
f.puts('$dat << eof')
ks = ['cmnl', 'nl']
f. puts(ks.join("\t"))
r = ARGV[0].to_f
2000.times \
{
l.sort_by! { |x| [x['nl'] - r * x['cmnl']] }
x = l.delete_at(0)
ns = x['ns']
f.puts([x.values_at(*ks)].join("\t"))
$stdout.flush
ns1 = ns.dup
ns1[0, 1] = '10'
ns2 = ns.dup
ns2[0, 1] = '11'
l << count(ns1)
l << count(ns2)
}
f.puts('eof')
f.puts("set title 'r=#{r}'; plot \\")
ks.each { |x| f.puts("$dat using (column('#{x}')) with line,\\") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment