红宝石挖掘工

Dig ruby with Pickaxe

ProjectEuler_Problem4

xavier posted @ 2010年11月24日 06:38 in Ruby成就 with tags projecteuler ruby , 5208 阅读

Find the largest palindrome made from the product of two 3-digit numbers.

找到能表示成两个三位数乘积的最大的回文数。

思路就是遍历所有三位数的乘积判断是不是回文数,再选出最大的来

result = 0
  999.downto(100) do |x|
    999.downto(100) do |y|
      if (x * y).to_s.reverse == (x * y).to_s
        result = [result, x*y].max
      end
    end
  end
p result

 

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter