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

NameTypeRequiredDefaultDescription
ANumericArrayYesReal positive integer scalar, vector, or array.
BNumericArrayYesReal positive integer scalar, vector, or array.

Returns

NameTypeDescription
LNumericArrayLeast common multiples of A and B.

Errors

IdentifierWhenMessage
RunMat:lcm:InvalidInputInputs are not real positive integer numeric values, or integer class mixing is unsupported.lcm: invalid input
RunMat:lcm:SizeMismatchInputs are neither the same size nor scalar-expandable.lcm: input sizes are not compatible
RunMat:lcm:OverflowThe least common multiple cannot be represented in the output numeric class.lcm: result overflows output type
RunMat:lcm:InternalGPU 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, and uint32) preserve their output class when paired with the same class or a double scalar.
  • Scalar signed integer and uint64 inputs 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.

Discrete

primes

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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref · vecnorm

Symbolic

digits · int · limit · piecewise · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how lcm is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.