Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created July 6, 2015 22:34
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/ff0c6a7a373a49276926 to your computer and use it in GitHub Desktop.
Save vznvzn/ff0c6a7a373a49276926 to your computer and use it in GitHub Desktop.
def f(n)
n2 = n
l = []
while (n >= n2)
l << n % 2
n = n.even? ? n / 2 : n * 3 + 1
end
return l
end
def match(l1, l, l2)
return if (l.empty?)
#$c = 0 if ($c.nil?)
#$c += 1
#return if ($c < 11)
li = (0...l.size).to_a.sort_by { |x| l[x..-1] }
while (!l1.empty?)
l1i = (0...l1.size).to_a.sort_by { |x| l1[x..-1] }
i = j = 0
l3 = []
while (i < l1i.size && j < li.size)
la = l1[l1i[i]..-1]
lb = l[li[j]..-1]
x = w = 0
y = li[j]
while (x < la.size && x < lb.size && la[x] == lb[x])
x += 1
w += l2[y]
end
l3 << [x, w, li[j], l1i[i]]
case (la <=> lb)
when -1
i += 1
when 0
i += 1
j += 1
when 1
j += 1
end
end
x = l3.sort_by { |x| x[0] * Math.sqrt(x[1]) }.reverse.first
# p(x)
c, w, j, i = x
puts([j, c, w].join("\t"))
$stdout.flush
# p(l1[i, c])
# p(l[j, c])
(j...j+c).each { |x| l2[x] += 1 }
l1[i, c] = []
# puts
end
end
n = 3
l = []
l2 = []
2000.times \
{
l1 = f(n)
match(l1.dup, l, l2)
l += l1
l2 += [1] * l1.size
n += 2
}
# p(l)
# p(l2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment