# Algorithm for Computing the LCM

The applet below illustrates an algorithm for finding the *Least Common Multiple* (*LCM*) of a number of integers.

Let there be a finite sequence of positive integers _{1}, x_{2}, ..., x_{n}),^{(m)} = (x_{1}^{(m)}, x_{2}^{(m)}, ..., x_{n}^{(m)}),^{(1)} = X.^{(m)}. Assuming x_{k0}^{(m)} is the selected element, the sequence X^{(m+1)} is defined as

x_{k}^{(m+1)} = x_{k}^{(m)}, k ≠ k_{0}

x_{k0}^{(m+1)} = x_{k0}^{(m)} + x_{k0}.

In other words, the least element is increased by the corresponding x whereas the rest of the elements pass from X^{(m)} to X^{(m+1)} unchanged.

The algorithm stops when all elements in sequence X^{(m)} are equal. Their common value L is exactly LCM(X).

(In the applet the numbers x_{1}, x_{2}, ..., x_{n} that appear at the top row are modifiable by clicking left or right off their vertical center line. Press button Compute to see the algorithm running. The blue numbers the ones that have been modified at one of the steps.)

What if applet does not run? |

|Contact| |Front page| |Contents| |Algebra|

Copyright © 1996-2017 Alexander Bogomolny

Let M^{k} = max x_{i}^{(k)}. There are only finitely many k such that ^{k} ≤ L.^{m} ≤ L^{m+1} > L

Indeed, by a suitable re-numbering, we will then have

X^{(m)} | = (r·x_{1}, x_{2}^{(m)}, ..., x_{n}^{(m)}) and | |

X^{(m+1)} | = ((r+1)·x_{1}, x_{2}^{(m)}, ..., x_{n}^{(m)}), |

with r a positive integer and

L ≠ r·x_{1} ≤ x_{2}^{(m)} ≤ ... ≤ x_{n}^{(m)} = M^{m} ≤ L

Now (r+l)·x_{1} ≤ L would imply that ^{m+1} ≤ L

r·x_{1} < L < (r+1)·x_{1}.

But this contradicts the fact that L is a multiple of x_{1}. Hence the assumption that m is not the last step is false and

X^{(m)}= (M^{m}, M^{m}, ..., M^{m}).

But then M^{m} is a common multiple of all the x_{i}, i = 1, 2, ..., n, and ^{m},^{m}.

L = M^{m}

and

X^{(m)}= (L, L, ..., L),

as required.

### Remark

This algorithm could be used to simulate a multitasking environment. In fact it was so used in the early days of computing. Assume, for example, you write a program which shows several balls bouncing up and down and off the walls. A task is associated with each ball that regularly updates its position and velocity. All balls must be continuously controlled, i.e. all the associated tasks need to be executed on a rotating basis. In addition, some balls may have to be updated more frequently than others.

The solution is this: each task k is assigned the (reversed) relative frequency x_{k} and the LCM algorithm is run. One step of task k is executed whenever x_{k}^{(m)} is updated. The above discussion shows that all tasks will be updated with the required frequencies.

It's not hard to imagine why the algorithm is thought to resemble a patch of growing onions which are cut individually when they reach a prescribed height. Indeed in networking there is an important *Onion Routing* algorithm. In computational geometry, a similar idea led to the *Onion Peeling Algorithm*.

### Reference

- A. J. Goldman; N. J. Fine,
__El655__,*The American Mathematical Monthly*, Vol. 71, No. 9. (Nov., 1964), pp. 1045-1046.

|Contact| |Front page| |Contents| |Algebra|

Copyright © 1996-2017 Alexander Bogomolny

62687580 |