C++ algorithm to calculate least common multiple for multiple numbers

后端 未结 15 1753
太阳男子
太阳男子 2020-12-14 16:29

Is there a C++ algorithm to calculate the least common multiple for multiple numbers, like lcm(3,6,12) or lcm(5,7,9,12)?

相关标签:
15条回答
  • 2020-12-14 17:01

    The algorithm isn't specific to C++. AFAIK, there's no standard library function.

    To calculate the LCM, you first calculate the GCD (Greatest Common Divisor) using Euclids algorithm.

    http://en.wikipedia.org/wiki/Greatest_common_divisor

    The GCD algorithm is normally given for two parameters, but...

    GCD (a, b, c) = GCD (a, GCD (b, c))
                  = GCD (b, GCD (a, c))
                  = GCD (c, GCD (a, b))
                  = ...
    

    To calculate the LCM, use...

                    a * b
    LCM (a, b) = ----------
                 GCD (a, b)
    

    The logic for that is based on prime factorization. The more general form (more than two variables) is...

                                              a                 b        
    LCM (a, b, ...) = GCD (a, b, ...) * --------------- * --------------- * ...
                                        GCD (a, b, ...)   GCD (a, b, ...)
    

    EDIT - actually, I think that last bit may be wrong. The first LCM (for two parameters) is right, though.

    0 讨论(0)
  • 2020-12-14 17:03
    • let the set of numbers whose lcm you wish to calculate be theta
    • let i, the multiplier, be = 1
    • let x = the largest number in theta
    • x * i
    • if for every element j in theta, (x*i)%j=0 then x*i is the least LCM
    • if not, loop, and increment i by 1
    0 讨论(0)
  • 2020-12-14 17:05

    The Codes given above only discusses about evaluating LCM for multiple numbers however it is very likely to happen that while performing multiplications we may overflow integer limit for data type storage

    *A Corner Case :- *

    e.g. if while evaluating you reach situation such that if LCM_till_now=1000000000000000 next_number_in_list=99999999999999 and Hence GCD=1 (as both of them are relatively co-prime to each other)

    So if u perform operation (LCM_till_now*next_number_in_list) will not even fit in "unsigned long long int"

    Remedy :- 1.Use Big Integer Class 2.Or if the problem is asking for LCM%MOD----------->then apply properties of modular arithmetic.

    0 讨论(0)
  • 2020-12-14 17:09

    I just created gcd for multiple numbers:

    #include <iostream>    
    using namespace std;
    int dbd(int n, int k, int y = 0);
    int main()
    {
        int h = 0, n, s;
        cin >> n;
        s = dbd(n, h);
        cout << s;
    }
    
    int dbd(int n, int k, int y){
            int d, x, h;
            cin >> x;
            while(x != y){
                if(y == 0){
                    break;
                }
                if( x > y){
                    x = x - y;
                }else{
                    y = y - x;
                }
            }
            d = x;
            k++;
            if(k != n){
            d = dbd(n, k, x);
            }
        return d;
    }
    

    dbd - gcd.

    n - number of numbers.

    0 讨论(0)
  • 2020-12-14 17:12

    If you look at this page, you can see a fairly simple algorithm you could use. :-)

    I'm not saying it's efficient or anything, mind, but it does conceptually scale to multiple numbers. You only need space for keeping track of your original numbers and a cloned set that you manipulate until you find the LCM.

    0 讨论(0)
  • 2020-12-14 17:15

    Using GCC with C++14 following code worked for me:

    #include <algorithm>
    #include <vector>
    
    std::vector<int> v{4, 6, 10};    
    auto lcm = std::accumulate(v.begin(), v.end(), 1, [](auto & a, auto & b) {
        return abs(a * b) / std::__gcd(a, b);
    });
    

    In C++17 there is std::lcm function (http://en.cppreference.com/w/cpp/numeric/lcm) that could be used in accumulate directly.

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