Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Last active February 3, 2018 02:00
Show Gist options
  • Save vznvzn/881ea44a9dac5aeaa80534085d4e7fe1 to your computer and use it in GitHub Desktop.
Save vznvzn/881ea44a9dac5aeaa80534085d4e7fe1 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s).to_i(2)
end
def stat(l)
l = [0] if (l.empty?)
t = t2 = 0
l.each \
{
|x|
t += x
t2 += x ** 2
}
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
return a, sd, l.max.to_f, l.min.to_f
end
def stat2(l, t, n)
return Hash[[["a#{n}", "sd#{n}", "mx#{n}"], stat(l)[0..2].map { |x| x / t }].transpose]
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def data(n)
ns = n.to_s(2)
nl = ns.length
m = nl / 2
nsh = ns[0..m]
nsl = ns[m..-1]
asdm1 = stat2(ns.split(/0+/).map { |x| x.length }, nl, 1)
l1 = ns.split(/1+/)
l1.shift
asdm0 = stat2(l1.map { |x| x.length }, nl, 0)
return {'n' => n, 'ns' => ns, 'nl' => nl, 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1).merge(asdm0)
end
def sum(l)
t = 0.0
l.each { |x| t += x }
return t
end
def out(fn, 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 plot(f, a, fn, c)
f.puts("plot \\")
(a['v'] + ['wr']).each \
{
|k|
f.puts("'#{fn}' using #{c}(column('#{k}_e')) with line title '#{k}',\\")
}
f.puts
f.puts("pause -1")
end
def count(n)
n1 = n
l = [n]
begin
n = f2(n)
l << n
end while n > n1
m = (0...l.size).max_by { |x| l[x] }
r = l[m].to_s(2).length - n1.to_s(2).length
return {'c' => l.size, 'n' => n1, 'l' => l, 'm' => m, 'r' => r}
end
def load(s)
raise if (s.nil?)
d = "out/#{s}"
l = Dir.glob("#{d}/*").select { |x| x =~ /out\d+.txt$/}
n = l.size
i = n - 1
a = Kernel.eval(File.open("#{d}/out#{i}.txt").readlines[0])
l = a['lb'].map { |x| x.size }
raise if (sum(l) != 0)
return a
end
def dist(c)
return (0...c).map { |i| count(dense(100, i.to_f / (c - 1))) }
end
def dot(x, z)
t = z['c']
(z.keys - ['c']).each { |v| t += z[v] * x[v] }
return t
end
def detect(n, a)
return $seen[n] if $seen.member?(n)
x1 = data(n)
i = 0
w = 1.0
x1['wr'] = 1.0
loop \
{
x2 = {}
(a['v'] + ['wr']).each \
{
|vy|
x2[vy] = dot(x1, a[vy].select { |k, x| (a['v'] + ['c']).member?(k) })
}
x1 = x2
w *= x1['wr']
i += 1
break if (w < 1.0 || i == 500)
}
$seen[n] = i
return i
end
a = load(ARGV[0])
l = dist(100)
l.sort_by! { |x| x['r'] }
l.reverse!
$seen = {}
l[0...10].each \
{
|y|
y['l'][0..y['m']].reverse.each \
{
|x|
puts([detect(x, a), x.to_s(2).length].join("\t"))
}
puts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment