>
>  Your task is to write a bit of Ruby code that will find and report all
>  integers that are divisible by their reverse. For example, 9801 is
>  divisible by 1089.
>
>
>

Here is my solution.
It generates numbers based on patterns and selects the numbers from
that list that are divisible by their reverse.
It will not work to infinity because the patterns are not complete.
I wish I had more time to clean up the code and make better patterns.

But it seems to work well with fairly large numbers and it is very fast.

###########
def revs(res,ul = 1000000)
arr = []
res.each {|y| arr << y if y % y.to_s.reverse.to_i == 0 and y.to_s !=
y.to_s.reverse}
return arr.uniq.sort.select {|x| x < ul}
end

ul = ARGV[0].to_i
res = []
arr3 = ["__87___9__12","__871287__12","__98___9__01","__980198__01"]
arr5 = ["__87___91287___9__12","__871287___91287__12","87128712___087128712",
        "__98___90198___9__01","__980198___90198__01","98019801___098019801"]

arr7 = ["__87___9__12___0__87___9__12","__87___91287___91287___9__12",
        "__98___9__01___0__98___9__01","__98___90198___90198___9__01"]

arr9 = ["__87__12___0__87___9__12___0__87__12",
        "__98__01___0__98___9__01___0__98__01"]
arr11 = ["__87___9__12___0__87___9__12___0__87___9__12",
         "__98___9__01___0__98___9__01___0__98___9__01"]

arr3.each do |x|
  (0..ul.to_s.length).each do |y|
    (0..ul.to_s.length).each do |z|
    res << x.unpack("@0a4" + "@4a4"*z + "@8a4").join.to_i
    end
  end
end

arr5.each do |x|
  (0..ul.to_s.length).each do |y|
    res << x.unpack("@0a4" + "@4a4" + "@8a4"*y + "@12a4" + "@16a4").join.to_i
    (0..ul.to_s.length).each do |z|
    res << x.unpack("@0a4" + "@4a4"*y + "@8a4"*z + "@12a4" * y +
"@16a4").join.to_i
    end
  end
end

arr7.each do |x|
  (0..ul.to_s.length).each do |y|
    (0..ul.to_s.length).each do |z|
    res << x.unpack("@0a4" + "@4a4"*y + "@8a4" + "@12a4"*z + "@16a4" +
"@20a4"*y + "@24a4").join.to_i
    end
  end
end

arr9.each do |x|
  (0..ul.to_s.length).each do |y|
    (0..ul.to_s.length).each do |z|
    res << x.unpack("@0a4"+"@4a4"+"@8a4"*y+"@12a4"+"@16a4"*z+"@20a4"+"@24a4"*y+"@28a4"+"@32a4").join.to_i
    end
  end
end

arr11.each do |x|
  (0..ul.to_s.length).each do |y|
    (0..ul.to_s.length).each do |z|
    res << x.unpack("@0a4"+"@4a4"*y+"@8a4"+"@12a4"*z+"@16a4"+"@20a4"*z+"@24a4"+"@28a4"*z+"@32a4"+"@36a4"*y+"@40a4").join.to_i
    end
  end
end


t = Time.now
outs = revs(res,ul) if ul > 0
outs = revs(res) if ul == 0
puts outs
puts
puts "#{Time.now - t} seconds"
###################

ruby rq161.rb
8712
9801
87912
98901
879912
989901

0.005498 seconds



ruby rq161.rb 100_000_000
8712
9801
87912
98901
879912
989901
8799912
9899901
87128712
87999912
98019801
98999901

0.040202 seconds



Harry

-- 
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html