C++ algorithm to calculate least common multiple f

2019-01-23 11:49发布

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条回答
欢心
2楼-- · 2019-01-23 12:20

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-23 12:21

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-23 12:21

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.

查看更多
混吃等死
5楼-- · 2019-01-23 12:24

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.

查看更多
家丑人穷心不美
6楼-- · 2019-01-23 12:27

Not built in to the standard library. You need to either build it yourself or get a library that did it. I bet Boost has one...

查看更多
The star\"
7楼-- · 2019-01-23 12:28

I found this while searching a similar problem and wanted to contribute what I came up with for two numbers.

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    cin >> x >> y;

    // zero is not a common multiple so error out
    if (x * y == 0)
        return -1;

    int n = min(x, y);
    while (max(x, y) % n)
        n--;

    cout << n << endl;
}
查看更多
登录 后发表回答