Created
March 18, 2017 02:54
-
-
Save vznvzn/b4cb8b5113ed778e6ad4762b7b2ef408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 >= n1 && n != 1) | |
n = f2(n) | |
l << n | |
end | |
x['ls'] = l.size | |
x['ns'] = x['n'].to_s(2).length | |
x['h2'] = (x['ls'].to_f / x['ns'] * 50).to_i | |
return x | |
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 init(w, f) | |
s = (0...w).map { rand(2).to_s }.join | |
s[0, 1] = '1' if (f) | |
return s | |
end | |
def dist(w) | |
l = [] | |
# f = false | |
f = true | |
20.times { l << adv({'nb' => init(w, f)}) } | |
c = 10e3.to_i | |
seen = {0 => nil} | |
i = n = 0 | |
while (n < c) | |
i += 1 | |
l.sort_by! { |x| -x['h2'] } | |
t = [l.size, 400].min | |
# 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") | |
return l | |
end | |
def hist(l) | |
h = {} | |
l.each \ | |
{ | |
|x| | |
h[x] = h.fetch(x, 0) + 1 | |
} | |
return h.sort | |
end | |
def range(l, k) | |
l1 = l.map { |x| x[k]} | |
a, sd, mx, mn = stat(l1) | |
h = hist(l1) | |
x = h.max_by { |k, v| v } | |
return {"#{k}_a" => a, "#{k}_sd" => sd, | |
"#{k}_mx" => mx, "#{k}_mn" => mn, | |
"#{k}_md" => x[0]} | |
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 | |
fn = 'out.txt' | |
File.open(fn, 'w').close | |
w = 100 | |
while (w >= 20) | |
l = dist(w) | |
s = range(l, 'h2') | |
out(fn, s.merge({ 'w' => w})) | |
$stderr.puts([w, s].inspect) | |
w -= 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment