I need an algorithm for LCM(k(adsbygoogle = window.adsbygoogle || []).push({}); _{1}, k_{2}, ..., k_{n}).

Here's what I was thinking:

- any number k
_{i}that divides evenly into another number k_{j}, set k_{i}= 1- return k
_{1}*k_{2}*...*k_{n}

I'm having trouble implementing it, though.

Code (Text):

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;

}

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Least Common Multiple of an arbitrary number of positive integers

Loading...

Similar Threads - Least Common Multiple | Date |
---|---|

C/++/# Is there a flaw in my longest common subsequence algorithm? | Feb 24, 2018 |

Python Two separate csv files with some common entries | Jan 13, 2018 |

How to code the expression with least roundoff error | Jan 29, 2016 |

Are these abbreviations common in programming/image processing? | Jan 15, 2015 |

Fortran77 woes- single parameter least squares minimisation stuck! | Jan 26, 2014 |

**Physics Forums - The Fusion of Science and Community**