Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 18, 2015 21:44
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/86c10c683e301fb5434b to your computer and use it in GitHub Desktop.
Save vznvzn/86c10c683e301fb5434b to your computer and use it in GitHub Desktop.
def f(n)
n1 = n
c = 0
while (n != 1 && n >= n1)
n = n * 3 + 1 if (n.odd?)
n /= 2 while (n.even?)
c += 1
end
return c
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
w = 1000
w.times \
{
|c|
l = [1] + (1..w - c).to_a.map { rand(2) } + [1] * c
n = l.join.to_i(2)
a = (0... (w - c / 2)).to_a
l = [0] * a.size
((w - c) / 2).times \
{
l[a.delete_at(rand(a.size))] = 1
}
n2 = ([1] + l + [1] * (c / 2)).join.to_i(2)
puts([c, f(n), f(n2), dense(n)].join("\t"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment