Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 26, 2016 00:24
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/119112fa532580320e72 to your computer and use it in GitHub Desktop.
Save vznvzn/119112fa532580320e72 to your computer and use it in GitHub Desktop.
def fs()
lo = [lambda { |n| n * 3 + 1 }, lambda { |n| (n * 3 + 1) / 2 },
lambda { |n| n = (n * 3 + 1) / 2 while (n.odd?); n }]
le = [lambda { |n| n / 2 }, lambda { |n| n /= 2 while (n.even?); n }]
l = []
lo.each \
{
|o|
le.each \
{
|e|
l << lambda \
{
|n|
n = o.call(n) if (n.odd?)
n = e.call(n) if (n.even?)
n
}
}
}
l[1, 1] = []
return l
end
def widthdiff(x, y)
x.to_s(2).length - y.to_s(2).length
end
def widthratio(x, y)
x.to_s(2).length.to_f / y.to_s(2).length
end
def f(n, f)
n1 = n
c = 0
l = [n]
cg = nil
while (n != 1)
n = f.call(n)
l << n
c += 1
cg = c if (cg.nil? && n < n1)
end
cm = (0...l.size).max_by { |x| l[x] }
return {'cm' => cm,
'cg' => cg.nil? ? 0 : cg,
'm' => l[cm].to_s(2).length,
'w' => widthdiff(l[cm], n1),
'r' => widthratio(l[cm], n1),
'c' => l.size}
end
fn = fs()
n = 3
l = []
50.times \
{
l2 = []
fs.each_with_index { |fn, i| z = f(n, fn); l2 << z.values.to_a }
l << l2
n += 2
}
a = 5
b = 6
l2 = []
5.times { |x| 6.times { |y| l2 << [x, y] } }
l2.reverse!
c = 0
l2.each_with_index \
{
|a, i|
x, y = a
l1 = []
l.each_with_index { |z, j| l1[j] = z[x][y] }
l1.each { |z| puts([c += 1, z, i].join("\t")) }
puts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment