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

后端 未结 14 1869

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:47

    Euclidean GCD code snippet

    int findGCD(int a, int b) {
            if(a < 0 || b < 0)
                return -1;
    
            if (a == 0)
                return b;
            else if (b == 0)
                return a;
            else 
                return findGCD(b, a % b);
        }
    

提交回复
热议问题