Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 3, 2018 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/80a687aa8e5ccca4f27a657fc5b70aa5 to your computer and use it in GitHub Desktop.
Save vznvzn/80a687aa8e5ccca4f27a657fc5b70aa5 to your computer and use it in GitHub Desktop.
def f1(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def f3(n)
return n.odd? ? (n * 3 + 1) : n / 2
end
def load()
fn = 'mixdb.txt'
s = File.stat(fn)
a = {'fn' => Dir.pwd + '/' + fn, 'ct' => s.ctime, 'mt' => s.mtime}
l = (f = File.open(fn)).readlines.map { |x| Kernel.eval(x) }
f.close
a['l_in'] = l.size
b = {}
l.each { |x| b[x['n']] = x }
l = b.values
a['l_uniq'] = l.size
$stderr.puts(a.inspect)
return l
end
def seq(n)
n1 = n
l = [n]
l2 = []
while (n >= n1 && n != 1)
l2 << n % 2
n = $f.call(n)
l << n
end
cm = (0...l.size).max_by { |x| l[x] }
return {'l' => l, 'l2' => l2.join, 'cm' => cm}
end
def sibling(n)
s = n.to_s(2)
s[0..1] = '1'
return s.to_i(2)
end
def out(a)
f = File.open($fn, 'a')
f.puts(a.keys.join("\t")) if (f.size == 0)
f.puts(a.values.join("\t"))
f.close
end
def review(n)
x1 = seq(n)
x2 = seq(sibling(n))
s1 = x1['l2']
s2 = x2['l2']
i = (0...[s1.length, s2.length].min).to_a.index { |x| s1[x, 1] != s2[x, 1] }
a = {'l1' => x1['l'].size, 'l2' => x2['l'].size, 'i' => i, 'cm' => x1['cm'], 'icm' => i.to_f / x1['cm'] - 1.0}
w = (a['icm'] <=> 0)
$c[w] = $c.fetch(w, 0) + 1
out(a)
end
tf = 2
$f = [lambda { |x| f1(x) }, lambda { |x| f2(x) }, lambda { |x| f3(x) }][tf]
File.open($fn = 'out.txt', 'w').close
l = load()
$c = {}
l.each { |x| review(x['n']) }
$stderr.puts($c.merge({'r' => ($c[1].to_f / l.size).round(3)}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment