Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 3, 2017 18:41
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/c496f984c802dfc1c32de60a2ad88993 to your computer and use it in GitHub Desktop.
Save vznvzn/c496f984c802dfc1c32de60a2ad88993 to your computer and use it in GitHub Desktop.
def f2(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
def adv(x)
x['n'] = x['nb'].to_i(2)
n1 = n = x['n']
l = [n]
while (n != 1)
n = f2(n)
l << n
end
x['l'] = l
x['ls'] = l.size
x['ns'] = x['n'].to_s(2).length
x['m'] = (0...l.size).max_by { |x| l[x] }
# x['y'] = (l.max.to_s(2).length.to_f / x['ns'] * 100).to_i
x['y'] = l.max.to_s(2).length
$h[x['y']] = [] if (!$h.member?(x['y']))
$h[x['y']] << x
# out1(data(x), ['a', 'sd', 'd', 'dl', 'dh', 'mn', 'y']) if (rand(100) == 0)
return x
end
def out1(a, l)
if ($f.nil?) then
$f = File.open('data.txt', 'w')
$f.puts(l.join("\t"))
end
$f.puts(a.values_at(*l).join("\t"))
$f.flush
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
end
def stat2(l, t, n)
return Hash[[["a#{n}", "sd#{n}", "mx#{n}"], stat(l).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(x)
n = x['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 {'y' => x['y'], 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm0).merge(asdm1)
end
def out(fn, l)
f = File.open("#{fn}.txt", 'w')
f.puts(l[0].keys.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.close
$stdout.puts("#{l.size} pts")
end
def init(w, f)
s = (0...w).map { rand(2).to_s }.join
s[0, 1] = '1' if (f)
return s
end
w = ARGV[0].nil? ? 50 : ARGV[0].to_i
$stdout.puts("#{w} width")
l = []
f = false
$h = {}
$c = 10
20.times { l << adv({'nb' => init(w, f)}) }
c = 50000
seen = {}
i = n = 0
while (n < c)
i += 1
l.sort_by! { |x| -x['y'] }
t = l.size / 10
case i % 3
when 0
x = l[rand(t)]
s = x['nb']
r = f ? (rand(s.length - 1) + 1) : rand(s.length)
s[r, 1] = (s[r, 1].to_i ^ 1).to_s
when 1
x = l[rand(t)]
y = l[rand(t)]
sx = x['nb']
sy = y['nb']
s = ''
w.times \
{
|j|
s[j, 1] = (rand(2) == 0) ? sx[j, 1] : sy[j, 1]
}
when 2
x = l[rand(t)]
y = l[rand(t)]
sx = x['nb']
sy = y['nb']
s = ''
r = rand(w + 1)
w.times \
{
|j|
s[j, 1] = (j < r) ? sx[j, 1] : sy[j, 1]
}
end
next if (seen.member?(s))
seen[s] = nil
l.pop if (l.size > 500)
l << adv({'nb' => s})
n += 1
end
$stderr.puts("#{i - n} dups")
l = []
$h.sort.each \
{
|k, v|
p([k, v.size])
}
$h.sort.select { |k, v| v.size >= $c }.each \
{
|k, v|
$c.times { l << data(v.delete_at(rand(v.size))) }
}
out('data', l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment