Least Common Multiple of an arbitrary number of positive integers

In summary, the conversation discusses the need for an algorithm for finding the least common multiple (LCM) of multiple numbers. The proposed idea is to set any number that divides evenly into another as 1 and then multiply all the remaining numbers together. However, there is trouble with implementation and a more efficient approach is suggested using Euclid's method of finding greatest common divisors. The example of k1=2*3, k2=2*5, k3=2*7 is given to show that the proposed idea may not always work.
  • #1
Jamin2112
986
12
I need an algorithm for LCM(k1, k2, ..., kn).

Here's what I was thinking:

  • any number ki that divides evenly into another number kj, set ki = 1
  • return k1*k2*...*kn

I'm having trouble implementing it, though.

Code:
int LCM(int* numsPtr, int size) { 
	// assume size > 1 and that array only contains non-negative numbers
	std::vector<int> numsVec(numsPtr, numsPtr + size);
	std::sort(numsVec.begin(), numsVec.begin() + size);
    for (int k = 0; k < size - 1; ++k) { 
		for (int j = k; j < size; ++j) {
			if (numsVec[j] % numsVec[k] == 0) {
				numsVec[k] = 1;
				break;
			}
		}
	}
	int product = 1;
	for (int k = 0; k < size; ++k) 
			product *= numsVec[k];
	return product;
}
 
Last edited:
Technology news on Phys.org
  • #2
What if you had k1=2*3, k2=2*5, k3=2*7? None evenly divides the other, but the LCM isn't k1*k2*k3, is it?
 
  • #3
The naive way is to factorize all the numbers.

A less naive way is to find their greatest common divisors. Some guy called Euclid found a neat way to do that.
 

FAQ: Least Common Multiple of an arbitrary number of positive integers

What is the Least Common Multiple (LCM) of a set of positive integers?

The LCM of a set of positive integers is the smallest positive integer that is divisible by all the numbers in the set. In other words, it is the smallest number that is a multiple of all the numbers in the set.

How is the LCM calculated?

The LCM of two integers can be calculated by finding the prime factorization of each number and then multiplying the highest power of each prime factor. For example, the LCM of 12 and 18 would be calculated as follows:
12 = 2^2 * 3
18 = 2 * 3^2
LCM = 2^2 * 3^2 = 36.

Can the LCM of a set of numbers be greater than the largest number in the set?

Yes, the LCM of a set of numbers can be greater than the largest number in the set. For example, the LCM of 4 and 6 is 12, which is greater than both numbers in the set.

What is the relationship between LCM and GCD?

The LCM and GCD (Greatest Common Divisor) are related by the following formula: LCM(a, b) * GCD(a, b) = a * b. This means that if you know the LCM and GCD of two numbers, you can find the original numbers by dividing the LCM by the GCD.

Can the LCM of a set of numbers be negative?

No, the LCM of a set of positive integers will always be a positive number. This is because the LCM is defined as the smallest positive integer that is divisible by all the numbers in the set.

Similar threads

Replies
15
Views
2K
Replies
75
Views
5K
Replies
25
Views
2K
Replies
1
Views
1K
Replies
6
Views
10K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
17
Views
2K
Back
Top