Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created July 28, 2016 00:51
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/b45288caf68caec3427664310f4b6376 to your computer and use it in GitHub Desktop.
Save vznvzn/b45288caf68caec3427664310f4b6376 to your computer and use it in GitHub Desktop.
def f1(n)
n = (n * 3 + 1) / 2 while (n % 2 == 1)
n /= 2 while (n % 2 == 0)
return n
end
def seq2(n)
l = [n]
n1 = n
begin
l << (n = f1(n))
end while (n >= n1 && n != 1)
return l
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return (0.5 - d).abs
end
def r(x)
return sprintf("%.3f", x).to_f
end
def mx(n)
l = seq2(n)
d = d(n.to_s)
mx = l.max
ns = n.to_s(2).length
v = mx.to_s(2).length.to_f / ns
return {'d' => d, 'ns' => ns, 'v' => v,
'ls' => l.size, 'h' => l.size.to_f / ns}
end
def range(l, k)
l1 = l.map { |x| x[k] }
mx = l1.max
mn = l1.min
r = mx == mn ? 1 : mx - mn
z = nil
l.each \
{
|x|
z = (x[k] - mn).to_f / r
i = (z * 10).to_i
i = 9 if i == 10
x[k + '_i'] = i
}
return z
end
def sizeof(l)
return l == nil ? 0 : l.size
end
def hard(c)
l = [{'n' => 1, 'p' =>0}.merge(mx(1))]
loop \
{
h2 = range(l, 'h')
v2 = range(l, 'v')
x = l[-1]
puts([l.size, h2, v2, x['h'], x['v'], x['ns'], x['ls']].join("\t"))
$stdout.flush
a = {}
l.each_with_index \
{
|x, i|
ij = [x['h_i'], x['v_i']]
a[ij] = [] if (a[ij].nil?)
a[ij] << i
}
ks = a.keys
ij = ks[rand(ks.size)]
i = a[ij][rand(a[ij].size)]
x = l.delete_at(i)
p = x['p'] + 1
n = x['n']
x1 = x.dup
x1['p'] = p
l.push(x1)
x2 = {}
n |= (1 << p)
x2['n'] = n
x2['p'] = p
x2.merge!(mx(n))
l.push(x2)
break if (l.size == c)
}
end
hard(5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment