Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
end
def back(n)
l = [n]
return (1..20).map \
{
l = l.map \
{
|x|
l1 = [x * 2]
l1 << (x - 1) / 3 if ((x - 1) % 3 == 0 && ((x - 1) / 3).odd?)
l1
}.flatten
l
}
end
def seq(n)
n1 = n
l = []
begin
l << n
n = f2(n)
end while (n != 1)
return l
end
def remove(a, ks)
a = a.dup
ks.each { |x| a.delete(x) }
return a
end
def count(n)
l = seq(n)
ns = n.to_s(2)
cm1 = (0...l.size).max_by { |x| l[x] }
cg = l.index { |x| x < l[0] }
cg = l.size if (cg.nil?)
cm = (0..cg).max_by { |x| l[x] }
npo = Math.log(l[ns.length])
return {
'n' => n,
'ns' => ns,
'cm' => cm,
'cm1' => cm1,
'ls' => l.size.to_f / 10,
'cg' => cg,
'nl' => Math.log(n),
'npo' => npo
}
end
def sum(l)
t = 0.0
l.each { |x| t += x }
return t
end
def avg(l)
return nil if (l.empty?)
return sum(l) / l.size
end
def graph(f, l2, t)
f.puts("$dat << eof")
f.puts(l2[0].keys.join("\t"))
j = 0
l2.each_with_index \
{
|x, c|
l = x['l2'].flatten
w = x['n'].to_s(2).length
l.each_with_index \
{
|x1, i|
f.puts([i, x1, c].join("\t"))
}
f.puts
}
f.puts('eof')
f.puts("set title '#{t}'; set key top left; plot $dat using 1:2:3 with point linecolor palette lw 1 pt 5 ps 0.25")
f.puts('pause -1')
end
def dense3(w, d)
begin
ns = dense(w, d)
n = ns.to_i(2)
end while (n % 3 == 0)
return n
end
def dist(k)
s = {}
l = []
1e4.to_i.times \
{
x = count(dense3(100, 0.5))
next if (s.member?(x[k]))
s[x[k]] = nil
l << x
}
return l.sort_by { |x| x[k] }
end
def sample(l, c)
return l if (l.size < c)
r = l.size / c
l2 = *(0...l.size).select { |x| x % r == 0 }
l2[c..-1] = []
return l.values_at(*l2)
end
def sample2(l, c)
s1 = l.size
l = sample(l, c)
s2 = l.size
$stderr.puts({'s1' => s1, 's2' => s2}.inspect)
return l
end
def trend2(x)
n = x['n']
l = back(n)
l2 = l.map { |l1| sample(l1.map { |x| Math.log(x) }, 10) }
return {'l2' => l2}.merge(count(n))
end
def process(f, l2, t)
l2.sort_by! { |x| x['cm'] }
graph(f, l2, t)
end
f = File.open('gnuplot.cmd', 'w')
c = 40
process(f, (1..c).map { trend2(count(dense3(100, 0.5))) }, 'dist1')
process(f, sample2(dist('cm'), c).map { |x| trend2(x) }, 'dist2')
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment