LoginSignup
3
3

More than 5 years have passed since last update.

Rubyで3桁の数で割り切れるかチェック

Posted at
# 3桁の数で割り切れるかチェック
def checkDivided(num)
  min = 111
  max = 999
  # 3桁の積でないものは弾く
  return false  if num < min ** 2  && num > max ** 2

  # minから順に割っていき、割り切れる&商が3桁ならtrue
  min.upto(max) do |x|
    if num % x == 0 && (num / x).to_s.length == 3
      return true
    end
  end

  return false
end
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3