When I searched for lcm c++, this thread had the top two spots, so I figured that I'd join and put this here.
For finding the LCM (Least Common Multiple) of two integers, a and b, in C++, use:
for(n=a;n%b != 0;n+=a);
For maximum speed, try to set it up so that the larger number is "a" and the smaller one is "b".
The least common multiple (LCM) of two numbers, a and b, is the smallest number that is a multiple of "a" AND a multiple of "b". If we already know that the LCM is a multiple of "a", then why not count by "a"? When we count by "a", starting at "a", we are testing every number that is a multiple of "a" already, and only have to see if it is a multiple of "b" too. This code does just that. When "a", the number we're counting by, is larger than 1, the process will be much faster than with those other suggestions, because it's taking bigger steps (at the same speed per step) to get to the same solution.
If "a" is a billion, then this will be at least a billion times faster than this:
for(n=1;n%a != 0 || n%b != 0;n++);
(even though both work)
I hope this helps.