def kaprekar(n, base = 10)
  return [1, 1, 1, ] if n == 1 
  return if n*(n-1) % (base-1)!= 0     
  sqr = (n ** 2).to_s(base)
  (1...sqr.length).each do |i|
    a = sqr[0 ... i]
    b = sqr[i .. -1]
    break if b.delete().empty?
    sum = a.to_i(base) + b.to_i(base)
    return n.to_s(base), sqr, a, b if sum == n
  end
  nil
end

count = 0
1.upto(10_000 - 1) do |i| 
  if result = kaprekar(i)
    puts  % result
    count += 1
  end
end

10_000.upto(1_000_000 - 1) {|i| count += 1 if kaprekar(i)}
puts 

puts 
base = 17
1.upto(1_000_000) do |decimal|
  if result = kaprekar(decimal, base)
    puts  % [decimal, *result]
  end
end