Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Least Common Multiple

  1. Jan 6, 2013 #1
    I'm reading this programming code that basically says if 1 of 2 variables (x and y) is less than 0, then their least common multiple is 0.

    If we have x=-2 and y=+3, then their least common multiple is 0.

    I don't get it. I know what a least common multiple is, but this one isn't very intuitive to me.

    Can someone explain?
  2. jcsd
  3. Jan 6, 2013 #2


    User Avatar
    Homework Helper

    That is not the usual definition. Usually lcm(-2,3)=6 and
  4. Jan 6, 2013 #3
    Well, it's just a definition. It's not the usual definition and it's probably not a very useful one either. So there is no explanation other that the authors of the code implemented things like this.
  5. Jan 6, 2013 #4
    Part of the code written in C is:
    if ( u < 0 || v < 0 )
    return 0;

    The overall code asks to find the lcm of u and v. If u or v is <0, then lcm of u and v is 0.
  6. Jan 6, 2013 #5


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Looks like the author considered it an error case and used a result of zero to signal an error condition back to the caller. This ought to be part of the declared interface. (But what does it return if an operand is 0?)
    If negative values are valid in your case, just wrap the operands in abs().
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook