lcm

Least common multiple of integers.

Category: Mathematics > Elementary Math

Syntax

L = lcm(A, B)

Returns the smallest positive integer that is a multiple of both A and B, element by element.

Description

The result equals abs(A.*B)./gcd(A, B). Signs are ignored. The function is what you need to add fractions with different denominators: the common denominator of 1/4 and 1/6 is lcm(4, 6) = 12.

With an array and a scalar, each element is paired with the scalar. To get the least common multiple of a whole vector, apply the function repeatedly, as in lcm(lcm(a, b), c).

Input arguments

A, B
Integer-valued arrays of compatible sizes. A scalar is expanded to match an array.

Output arguments

L
Array of nonnegative integers. If either input is 0 the result is 0. Integer inputs of one class give a result of that class.

Example

Pairs and arrays

lcm(4, 6)
lcm([4 6 10], 15)
lcm(0, 5)
Output
ans = 12
ans = 1×3

    60    30    30
ans = 0

See also

gcd factor primes rat