lcm — Compute least common multiples for positive integer inputs.
lcm(A,B) returns the least common multiples of corresponding elements of A and B. Inputs must be real positive integer values, and the inputs must be the same size or one input must be scalar.
Syntax
L = lcm(A, B)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | NumericArray | Yes | — | Real positive integer scalar, vector, or array. |
B | NumericArray | Yes | — | Real positive integer scalar, vector, or array. |
Returns
| Name | Type | Description |
|---|---|---|
L | NumericArray | Least common multiples of A and B. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:lcm:InvalidInput | Inputs are not real positive integer numeric values, or integer class mixing is unsupported. | lcm: invalid input |
RunMat:lcm:SizeMismatch | Inputs are neither the same size nor scalar-expandable. | lcm: input sizes are not compatible |
RunMat:lcm:Overflow | The least common multiple cannot be represented in the output numeric class. | lcm: result overflows output type |
RunMat:lcm:Internal | GPU gather or tensor construction fails. | lcm: internal error |
How lcm works
- Accepts exactly two inputs.
- Inputs must contain finite, real, positive integer values.
- Inputs must have the same size or one input must be scalar-expanded.
- Double inputs return double results; single inputs mixed with double return single results.
- Supported unsigned integer array inputs (
uint8,uint16, anduint32) preserve their output class when paired with the same class or a double scalar. - Scalar signed integer and
uint64inputs preserve their scalar integer class when the result is representable. - Mixed integer classes are rejected, matching MATLAB's requirement that integer inputs use the same class unless paired with a double scalar.
- Logical, complex, zero, negative, fractional, non-finite, shape-mismatched, and overflowing inputs raise errors.
Examples
Least common multiples of a double array and a scalar
A = [5 17; 10 60];
L = lcm(A,45)Expected output:
L = [45 765; 90 180]Preserve unsigned integer output class
A = uint16([255 511 15]);
B = uint16([15 127 1023]);
L = lcm(A,B)Expected output:
L = uint16([255 64897 5115])Using lcm with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how lcm changes the result.
Run a small lcm example, explain the result, then change one input and compare the output.
FAQ
Does lcm support implicit expansion?⌄
RunMat supports MATLAB-compatible scalar expansion. Non-scalar inputs must have the same size.
Why does lcm reject mixed integer classes?⌄
MATLAB requires integer inputs to have the same class unless the other input is a double scalar. RunMat follows that rule to avoid silently returning the wrong integer class.
Why does lcm raise overflow errors for integer outputs?⌄
The result must fit the output class. RunMat rejects overflowing integer results instead of silently truncating them.
Related Math functions
Discrete
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · uint32
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Optim
coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimset · quad · secondordercone
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how lcm is executed, line by line, in Rust.
- View the source for lcm in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.