Skip to content

Instantly share code, notes, and snippets.

@vznvzn

vznvzn/spread5.rb

Created May 17, 2019
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 seq(n)
l = [n]
while (n != 1)
n = f2(n)
l << n
end
return l
end
def dv(l)
a, b = [l[0], l[-1]]
l2 = []
l.size.times \
{
|x|
y = a + x.to_f / (l.size - 1) * (b - a)
dv = (l[x] - y)
l2 << dv.abs
}
return l2
end
def norm(l)
c = 100
l2 = []
c.times \
{
|x|
i = (x.to_f / (c - 1) * (l.size - 1)).to_i
l2 << l[i]
}
return l2
end
def avg(l)
l.inject { |t, x| t + x }.to_f / l.size
end
l2 = []
500.times \
{
n = dense(100, 0.5).to_i(2)
l = seq(n).map { |x| Math.log(x) }
l2 << norm(dv(l))
}
l1 = l2.transpose.map { |x| avg(x) }
l1.each { |x| p(x) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment