How to check if x is a multiple of y

T

Tom Ha

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
 
S

Stefano Crocco

|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

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

if (x%y) == 0
...

Stefano
 
X

Xavier Noria

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.
 
R

Robert Klemme

2010/7/21 Xavier Noria said:
That's typically done with the modulus operator, Active Support
defines it this way

=A0 =A0class Integer
=A0 =A0 =A0# Check whether the integer is evenly divisible by the argumen= t.
=A0 =A0 =A0def multiple_of?(number)
=A0 =A0 =A0 =A0number !=3D 0 ? self % number =3D=3D 0 : zero?
=A0 =A0 =A0end
=A0 =A0end

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 =3D=3D 0
end
end

:)

Don't have the time right now...

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
X

Xavier Noria

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

class Integer
=C2=A0def multiple_off? number
=C2=A0 =C2=A0zero? || self % number =3D=3D 0
=C2=A0end
end

:)

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

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

There you see why posting in a hurry is a bad idea. Thanks for catching
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top