How to check if x is a multiple of y

Hi there,

what’s the most simple solution in Ruby to check if value x is a
multiple of y?

“if x/y.integer?”

…is obviously not the solution.

Thanks a lot!
Tom

On Wednesday 21 July 2010, Tom Ha wrote:

|Tom
Assuming x and y are both integers, you can use the modulo operator:

if (x%y) == 0

Stefano

On Wed, Jul 21, 2010 at 12:12 PM, Tom Ha [email protected] wrote:

what’s the most simple solution in Ruby to check if value x is a
multiple of y?

That’s typically done with the modulus operator, Active Support
defines it this way

class Integer
  # Check whether the integer is evenly divisible by the argument.
  def multiple_of?(number)
    number != 0 ? self % number == 0 : zero?
  end
end

It is special-cased because you can’t do modulus 0.

Thanks to everybody !

2010/7/21 Xavier N. [email protected]:

 def multiple_of?(number)
   number != 0 ? self % number == 0 : zero?
 end

end

It is special-cased because you can’t do modulus 0.

I see a benchmark lurking: that version vs. this one.

class Integer
def multiple_off? number
zero? || self % number == 0
end
end

:slight_smile:

Don’t have the time right now…

Cheers

robert

x = 9
y = 3

puts “yep” if (x%y).zero?

x = 9
y = 2

puts “nope” if !(x%y).zero?

On Wednesday, July 21, 2010, Robert K. [email protected]
wrote:

   # Check whether the integer is evenly divisible by the argument.
 def multiple_off? number
  zero? || self % number == 0
 end
end

:slight_smile:

But that one is not well-defined for 1.multiple_of?(0) :slight_smile:

On 21.07.2010 13:20, Xavier N. wrote:

class Integer

class Integer
def multiple_off? number
zero? || self % number == 0
end
end

:slight_smile:

But that one is not well-defined for 1.multiple_of?(0) :slight_smile:

There you see why posting in a hurry is a bad idea. Thanks for catching
that.

Cheers

robert

On Wed, Jul 21, 2010 at 6:45 PM, Robert K.
[email protected]wrote:

zero? || self % number == 0

that.
For what they are worth (which is very little as I distrust benchmarks
which
haven’t been run sufficiently many times to generate reliable standard
deviations, not to mention my Windows Vista installation doing even more
apparently random and purposeless disk i/o than usual) these benchmarks
suggest that (ignoring the minor bug!) multiple_off? was usually
slightly
slower than multiple_of? on my system about two hours ago!

ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
each benchmark is a 1_000_0000.times run;

                user     system      total        real

num= 72 denom= 13

multiple_of? 0.921000 0.000000 0.921000 ( 1.012000)
multiple_off? 0.967000 0.000000 0.967000 ( 1.177000)

multiple_of? 0.827000 0.015000 0.842000 ( 0.992000)
multiple_off? 0.983000 0.000000 0.983000 ( 1.110000)

num= 72 denom= 12

multiple_of? 0.874000 0.000000 0.874000 ( 1.072000)
multiple_off? 0.967000 0.000000 0.967000 ( 1.182000)

multiple_of? 0.936000 0.000000 0.936000 ( 1.090000)
multiple_off? 1.030000 0.000000 1.030000 ( 1.156000)

num= 0 denom= 0

multiple_of? 0.874000 0.000000 0.874000 ( 1.108000)
multiple_off? 0.780000 0.000000 0.780000 ( 1.004000)

multiple_of? 0.827000 0.000000 0.827000 ( 1.122000)
multiple_off? 0.889000 0.000000 0.889000 ( 1.043000)

num= 72 denom= 0

multiple_of? 0.905000 0.000000 0.905000 ( 1.114000)
multiple_of? 0.936000 0.000000 0.936000 ( 1.088000)

multiple_off?
#<ZeroDivisionError: divided by 0>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs