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

后端 未结 14 1884

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

    Take successive multiples of the larger of the two numbers until the result is a multiple of the smaller.

    this might work..

       public int LCM(int x, int y)
       {
           int larger  = x>y? x: y,
               smaller = x>y? y: x,
               candidate = larger ;
           while (candidate % smaller  != 0) candidate += larger ;
           return candidate;
       }
    
    0 讨论(0)
  • 2020-12-07 13:41

    C++ template. Compile time

    #include <iostream>
    
    const int lhs = 8, rhs = 12;
    
    template<int n, int mod_lhs=n % lhs, int mod_rhs=n % rhs> struct calc {
      calc() { }
    };
    
    template<int n> struct calc<n, 0, 0> {
      calc() { std::cout << n << std::endl; }
    };
    
    template<int n, int mod_rhs> struct calc<n, 0, mod_rhs> {
      calc() { }
    };
    
    template<int n, int mod_lhs> struct calc <n, mod_lhs, 0> {
      calc() { }
    };
    
    template<int n> struct lcm {
      lcm() {
        lcm<n-1>();
        calc<n>();
      }
    };
    
    template<> struct lcm<0> {
      lcm() {}
    };
    
    int main() {
      lcm<lhs * rhs>();
    }
    
    0 讨论(0)
  • 2020-12-07 13:42

    The most efficient way to calculate LCM - Time Complexity - O( log(min(a,b)) )

    Fundamental Formula - LCM(a,b) = (a*b) / GCD(a,b)

    The time comlexity of GCD(a,b) is O( log(min(a,b)) )

    Here you can find the code in C, C++, C#, Python etc. click here

    Thanks !

    0 讨论(0)
  • 2020-12-07 13:47

    I don't know whether it is optimized or not, but probably the easiest one:

    public void lcm(int a, int b)
    {
        if (a > b)
        {
            min = b;
            max = a;
        }
        else
        {
            min = a;
            max = b;
        }
        for (i = 1; i < max; i++)
        {
            if ((min*i)%max == 0)
            {
                res = min*i;
                break;
            }
        }
        Console.Write("{0}", res);
    }
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
  • 2020-12-07 13:48

    The least common multiple (lcm) of a and b is their product divided by their greatest common divisor (gcd) ( i.e. lcm(a, b) = ab/gcd(a,b)).

    So, the question becomes, how to find the gcd? The Euclidean algorithm is generally how the gcd is computed. The direct implementation of the classic algorithm is efficient, but there are variations that take advantage of binary arithmetic to do a little better. See Knuth's "The Art of Computer Programming" Volume 2, "Seminumerical Algorithms" § 4.5.2.

    0 讨论(0)
提交回复
热议问题