码迷,mamicode.com
首页 > 其他好文 > 详细

Epic - Seed Number

时间:2015-06-14 15:05:22      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

Find the seed of a number.

Eg : 1716 = 143*1*4*3 =1716 so 143 is the seed of 1716. find all possible seed for a given number.

辗转相除法,由性质可利用 sqrt(num) <= seed_number <= num/2 缩小范围。

def seed_number(num)
  seed = Math.sqrt(num).to_i
  while seed <= num / 2
    if num % seed == 0
      product = temp = seed
      while temp != 0
        product *= temp%10
        temp /= 10
      end
      return seed if product == num
    end
    seed += 1
  end
end

 

Epic - Seed Number

标签:

原文地址:http://www.cnblogs.com/lilixu/p/4575028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!