gcd
Greatest common divisor of integers.
Category: Mathematics > Elementary Math
Syntax
G = gcd(A, B)
Returns the largest positive integer that divides both A and B, element by element.
Description
The computation uses the Euclidean algorithm: replace the pair (a, b) by (b, mod(a, b)) until the second number is zero. Signs are ignored, since the divisor is always taken positive.
To reduce a fraction p/q to lowest terms, divide both by gcd(p, q). The least common multiple is related by lcm(a, b) = abs(a*b)/gcd(a, b).
Input arguments
A, B- Integer-valued arrays of compatible sizes. A scalar is expanded to match an array.
Output arguments
G- Array of nonnegative integers.
gcd(0, 0)is0, andgcd(0, n)isabs(n). Integer inputs of one class give a result of that class.
Example
Scalars and arrays
gcd(12, 18)
gcd([12 15 20], 5)
gcd(0, 7)
Output
ans = 6
ans = 1×3
1 5 5
ans = 7