lcm

Least common multiple

Description

example

lcm(A) finds the least common multiple of all elements of A.

example

lcm(A,B) finds the least common multiple of A and B.

Examples

Least Common Multiple of Four Integers

To find the least common multiple of three or more values, specify those values as a symbolic vector or matrix.

Find the least common multiple of these four integers, specified as elements of a symbolic vector.

A = sym([4420, -128, 8984, -488])
lcm(A)
A =
[ 4420, -128, 8984, -488]

ans =
9689064320

Alternatively, specify these values as elements of a symbolic matrix.

A = sym([4420, -128; 8984, -488])
lcm(A)
A =
[ 4420, -128]
[ 8984, -488]

ans =
9689064320

Least Common Multiple of Rational Numbers

lcm lets you find the least common multiple of symbolic rational numbers.

Find the least common multiple of these rational numbers, specified as elements of a symbolic vector.

lcm(sym([3/4, 7/3, 11/2, 12/3, 33/4]))
ans =
924

Least Common Multiple of Complex Numbers

lcm lets you find the least common multiple of symbolic complex numbers.

Find the least common multiple of these complex numbers, specified as elements of a symbolic vector.

lcm(sym([10 - 5*i, 20 - 10*i, 30 - 15*i]))
ans =
- 60 + 30i

Least Common Multiple of Elements of Matrices

For vectors and matrices, lcm finds the least common multiples element-wise. Nonscalar arguments must be the same size.

Find the least common multiples for the elements of these two matrices.

A = sym([309, 186; 486, 224]);
B = sym([558, 444; 1024, 1984]);
lcm(A,B)
ans =
[  57474, 13764]
[ 248832, 13888]

Find the least common multiples for the elements of matrix A and the value 99. Here, lcm expands 99 into the 2-by-2 matrix with all elements equal to 99.

lcm(A,99)
ans =
[ 10197,  6138]
[  5346, 22176]

Least Common Multiple of Polynomials

Find the least common multiple of univariate and multivariate polynomials.

Find the least common multiple of these univariate polynomials.

syms x
lcm(x^3 - 3*x^2 + 3*x - 1, x^2 - 5*x + 4)
ans =
(x - 4)*(x^3 - 3*x^2 + 3*x - 1)

Find the least common multiple of these multivariate polynomials. Because there are more than two polynomials, specify them as elements of a symbolic vector.

syms x y
lcm([x^2*y + x^3, (x + y)^2, x^2 + x*y^2 + x*y + x + y^3 + y])
ans =
(x^3 + y*x^2)*(x^2 + x*y^2 + x*y + x + y^3 + y)

Input Arguments

collapse all

Input value, specified as a number, symbolic number, variable, expression, function, or a vector or matrix of numbers, symbolic numbers, variables, expressions, or functions.

Input value, specified as a number, symbolic number, variable, expression, function, or a vector or matrix of numbers, symbolic numbers, variables, expressions, or functions.

Tips

• Calling lcm for numbers that are not symbolic objects invokes the MATLAB® lcm function.

• The MATLAB lcm function does not accept rational or complex arguments. To find the least common multiple of rational or complex numbers, convert these numbers to symbolic objects by using sym, and then use lcm.

• Nonscalar arguments must have the same size. If one input arguments is nonscalar, then lcm expands the scalar into a vector or matrix of the same size as the nonscalar argument, with all elements equal to the corresponding scalar.