Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 18, 2015 16:48
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/4fec912ed7e4ec644f8c to your computer and use it in GitHub Desktop.
Save vznvzn/4fec912ed7e4ec644f8c to your computer and use it in GitHub Desktop.
def power(x, p)
n = n1 = x ** p
w = n.to_s(2).length
c = 0
while (n != 1 && n >= n1)
n = n * 3 + 1 if (n.odd?)
n /= 2 while (n.even?)
c += 1
end
return w, c, dense(n1), dense(x)
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 out(x, c)
w = x.to_s(2).split(/0+/)
l = []
50.times \
{
|i|
l << power(x, i * 2 + 1) + [w[-1].length]
}
return l
end
def stat(l)
return 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)
return f3(a), f3(sd), c
end
def f3(x)
sprintf("%.3f", x).to_f
end
t = 1024 + 3
l = []
50.times \
{
|i|
l += out(t, i)
t += 4
}
l2 = []
c = 4
l.each \
{
|x|
p = x[4]
l2[p] = [] if (l2[p].nil?)
l2[p] << x
}
l2.each \
{
|l|
next if (l.nil?)
p(stat(l.map { |x| x[1] }))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment