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(ns)
l = seq(n = ns.to_i(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] }
return {
'ns' => ns,
'cm' => cm,
'cm1' => cm1,
'ls' => l.size.to_f / 10,
'cg' => cg,
'nl' => Math.log(n)
}
end
def trend(n)
l = back(n).map { |x| {'mn' => Math.log(x.min), 'mx' => Math.log(x.max), 'r' => Math.log(x.max) - Math.log(x.min)} }
mn = l.map { |x| x['mn'] }.min
mx = l.map { |x| x['mx'] }.max
rmn = l.map { |x| x['r'] }.min
rmx = l.map { |x| x['r'] }.max
return {'n3' => n % 3,
'n2' => n % 2,
'n' => Math.log(n),
'mn' => mn,
'mx' => mx,
'rmn' => rmn,
'rmx' => rmx,
'rrmn' => mn / Math.log(n),
}.merge(count(n.to_s(2)))
end
l2 = []
1000.times { l2 << trend(dense(100, 0.5).to_i(2)) }
f = File.open('gnuplot.cmd', 'w')
f.puts("$dat << eof")
f.puts(l2[0].keys.join("\t"))
l2.each { |x| f.puts(x.values.join("\t")) }
f.puts("eof")
['cm', 'cm1', 'cg', 'ls'].each_with_index \
{
|x, c|
f.puts("plot $dat using (column('rrmn')):(column('#{x}')) title '#{x}' ps 0.5 pt 5 lc #{c + 1}")
f.puts('pause -1')
}
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment