Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created November 13, 2015 22:32
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/6ee862e14437e8b2045c to your computer and use it in GitHub Desktop.
Save vznvzn/6ee862e14437e8b2045c to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby1.8
def fs()
lo = [lambda { |n| n * 3 + 1 }, lambda { |n| (n * 3 + 1) / 2 },
lambda { |n| n = (n * 3 + 1) / 2 while (n.odd?); n }]
le = [lambda { |n| n / 2 }, lambda { |n| n /= 2 while (n.even?); n }]
l = []
lo.each \
{
|o|
le.each \
{
|e|
next if (o == lo.last && e == le.last)
l << lambda \
{
|n|
n = o.call(n) if (n.odd?)
n = e.call(n) if (n.even?)
n
}
}
}
return l
end
def dense(n)
s = n.to_s(2)
c = 0
s.length.times \
{
|i|
c += s[i, 1].to_i
}
return c.to_f / s.length
end
def widthdiff(x, y)
x.to_s(2).length - y.to_s(2).length
end
def widthratio(x, y)
x.to_s(2).length.to_f / y.to_s(2).length
end
def f(n, w)
n1 = n
c = 0
l = [n]
cg = nil
while (n != 1)
n = $f.call(n)
l << n
c += 1
cg = c if (cg.nil? && n < n1)
break if (!cg.nil? && w != 'c')
end
cm = (0...l.size).max_by { |x| l[x] }
return {'cm' => cm,
'cg' => cg.nil? ? 0 : cg,
'm' => l[cm].to_s(2).length,
'w' => widthdiff(l[cm], n1),
'r' => widthratio(l[cm], n1),
'c' => l.size,
'd' => (0.5 - dense(n1)).abs}
end
def long(m, w)
return {'n' => rand(2 ** m) | 1 } if (w == 'z')
l = [{'n' => 1, 'p' => 0}.merge(f(1, w))]
t = 0
e = 2000
loop \
{
l = l.sort_by { |x| [x[w], -x['d']] }.reverse
x = l.delete_at(rand([l.size, 20].min))
p2 = x['p'] + 1
n2 = x['n'] + 2 ** p2
x1 = x.dup
x1['p'] = p2
l << x1
x2 = {'n' => n2, 'p' => p2}.merge(f(n2, w))
l << x2
t += 1
w2 = w
if (w == 'r') then
z = l.sort_by { |x| x['n'] }.reverse[0...100].max_by { |x| x['r'] }
w2 = 's'
else
z = l.max_by { |x| x[w] }
end
z.merge!({ 't' => t, 's' => t.to_f / e })
return z if (z[w2] >= m)
return if (t >= e)
}
end
def mx(a, b)
a.nil? ? b : [a, b].max
end
def mn(a, b)
a.nil? ? b : [a, b].min
end
def stat(l)
return 0, 0, 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)
raise if (sd.nan?)
return a, sd, l.max.to_f
end
def nonmono(l)
mn = nil
r = nil
l.each_with_index \
{
|d, i|
d1 = d.abs
mn = mn(mn, [d1, i])
r = mx(r, i - mn[1])
}
return r
end
def run1(n)
n1s = n.to_s(2).length
l = []
while (n != 1)
n = $f.call(n)
l << n
end
d2 = 0
l2 = []
l.each \
{
|x|
d = dense(x) - 0.5
l2 << d
break if (d * d2 < 0)
d2 = d
}
cm = (0...l.size).max_by { |j| l[j] }
return if (cm.nil?)
la = []
lb = []
lc = []
ld = []
if (cm > l2.size) then
(l2.size...cm).each \
{
|j|
n = l[j]
ns = n.to_s(2)
m0 = stat(ns.split(/1+/).map { |x| x.length })
m1 = stat(ns.split(/0+/).map { |x| x.length })
la << -m1[0]
lb << -m1[1]
lc << -m1[2]
r0 = m1[0] / m1[1]
ld << r0
}
end
puts([n1s, cm - l2.size, [la, lb, lc, ld].map { |l| nonmono(l) }].join("\t"))
$stdout.flush
end
def test(p, k, m)
r = p[k]
a, b, c = r
return false if (c && m >= 1.0)
x = a + m * (b - a)
z = long(x, k)
return false if (z.nil?)
$stderr.puts([k, r2(m), r2(x), z['s'], z['n'].to_s(2)].inspect)
run1(z['n'])
return true
end
def r2(x)
sprintf("%.2f", x).to_f
end
def incr(p, k)
c = 0
w = 0.0
loop \
{
|i|
if (!test(p, k, w)) then
$stderr.puts("***")
c += 1
break if (c == 3)
end
w += 0.01
}
20.times { puts(([0] * 6).join("\t")) }
end
p = {'w' => [0, 80, false],
'p' => [0, 350, false],
'c' => [0, 550, false],
'm' => [0, 300, false],
'cg' => [0, 140, false],
'cm' => [0, 50, false],
'r' => [0.0, 1.0, true],
'z' => [0, 1000, true],
}
$f = fs()[4]
#test(p, 'cm', 0.5); exit
ARGV[0].nil? ? p.keys.each { |k| incr(p, k) } : incr(p, ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment