Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Find if a number is a multiple of a number.

by Baratski (Acolyte)
on Mar 02, 2005 at 12:29 UTC ( #435792=perlquestion: print w/ replies, xml ) Need Help??
Baratski has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

Is there a better way, other than my following example, to find if a number is a multiple of a number?

My klunky way would be to keep subtracting x from y and look for a zero value. This seems like a long haul if I have to keep subtracting say, 4 from 123456789, or something like that.

$x = 4; $y = 123456789; while($y > 0) { $y -= $x; if($y == 0) { #..... } }

Is there a better way?

Thank you.

Comment on Find if a number is a multiple of a number.
Download Code
Re: Find if a number is a multiple of a number.
by gellyfish (Monsignor) on Mar 02, 2005 at 12:36 UTC

    You can use the modulus operator :

    if ( ! ($y % $x) ) { # it is an exact multiple }

    /J\

Re: Find if a number is a multiple of a number.
by Anonymous Monk on Mar 02, 2005 at 12:36 UTC
    Use the modulus operator: %. For details, see the perlop manual page.
Re: Find if a number is a multiple of a number.
by g0n (Priest) on Mar 02, 2005 at 12:36 UTC
    Could you be looking for the modulus operator '%'?

    perl -e 'print 123456789%4'

    VGhpcyBtZXNzYWdlIGludGVudGlvbmFsbHkgcG9pbnRsZXNz
Re: Find if a number is a multiple of a number.
by borisz (Canon) on Mar 02, 2005 at 12:41 UTC
    use % for positive integer values.
    $x = 4; $y = 12345679; if ( $y % $x == 0 ) { ... }
    Boris
      Reply to all. Sweeeet! Works great! And much, much faster than my code. Thanks!
      use % for positive integer values.
      What does make you think that the use % of is limited to positive integers?
      $ perl -le 'print for map $_%-3, -18..-15' 0 -2 -1 0
        The docs: from perlop
        Binary "%" computes the modulus of two numbers.Given integer operands $a and $b: If $b is positive, then "$a % $b" is $a minus the largest multiple of $b that is not greater than $a. If $b is negative then "$a % $b" is $a minus the smallest multiple of $b that is not less tha +n $a (i.e. the result will be less than or equal to zero). Note that when "use integer" is in scope "%" gives you direct access to the mod- ulus operator as implemented by your C compiler. This operator is not as well defined for negative operands, but it will execute faster.
        Boris
Re: Find if a number is a multiple of a number.
by ambrus (Abbot) on Mar 02, 2005 at 13:06 UTC
    Use
    sub divides1 { my($d, $n) = @_; $d < $n ? $d & 1 ? $n & 1 ? divides($d, $n - $d) : divides($d, $n >> 1) : $n & 1 ? 0 : divides($d >> 1, $n >> 1) : $d == $n; } sub divides { 0 == $_[1] || divides1(abs $_[0], abs $_[1]) }
    or simply
    sub divides { 0 == $_[0] ? 0 == $_[1] : 0 == $_[1] % $_[0] }

    Update: fixed newline in divides1 subroutine.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://435792]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-12-19 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (71 votes), past polls