What is the most efficient way to calculate the least common multiple of two integers?

后端 未结 14 1887

What is the most efficient way to calculate the least common multiple of two integers?

I just came up with this, but it definitely leaves something to be desired.

14条回答
  •  臣服心动
    2020-12-07 13:59

    There is no way more efficient than using a built-in function!

    As of Python 3.8 lcm() function has been added in math library. And can be called with folowing signature:

    math.lcm(*integers)
    

    Returns the least common multiple of the specified integer arguments. If all arguments are nonzero, then the returned value is the smallest positive integer that is a multiple of all arguments. If any of the arguments is zero, then the returned value is 0. lcm() without arguments returns 1.

提交回复
热议问题