def iterated_square_digit(d)
  f = Array.new(d+1){|n| (1..n).inject(1,:*)}      
  g = -> (n) { res = n.digits.sum{|d| d*d} 
               res==89? 0: res }

  
  table = Array.new(d*81+1){|n| n.zero?? 1: (i=g.call(n))==89? 0: i}
  table.collect!{|n| n = table[n] while n>1; n}
  z = 0                                             
  [*0..9].repeated_combination(d) do |rc|           
    next if table[rc.inject(0){|g,n| g+n*n}].zero?  
    nn = [0] * 10                                   
    rc.each{|n| nn[n] += 1}
    z += nn.inject(f[d]){|gn,n| gn / f[n]}          
  end
  puts ,
       
end

[8, 11, 14, 17].each do |d|
  t0 = Time.now
  iterated_square_digit(d)
  puts 
end