(3) Greatest common divisor

advertisement
(3)
Greatest common divisor
In mathematics, the greatest common divisor (gcd), also known as the greatest
common factor (gcf), or highest common factor (hcf), of two or more non-zero
integers, is the largest positive integer that divides the numbers without a remainder. For
example, the GCD of 8 and 12 is 4.
This notion can be extended to polynomials, see greatest common divisor of two
polynomials.
Overview
Example
The number 54 can be expressed as a product of two other integers in several different
ways:
Thus the divisors of 54 are:
Similarly the divisors of 24 are:
The numbers that these two lists share in common are the common divisors of 54 and
24:
The greatest of these is 6. That is the greatest common divisor of 54 and 24. One writes:
Reducing fractions
The greatest common divisor of a and b is written as gcd(a, b), or sometimes simply as
(a, b). For example, gcd(12, 18) = 6, gcd(−4, 14) = 2.
1
Two numbers are called relatively prime, or coprime if their greatest common divisor
equals 1. For example, 9 and 28 are relatively prime.
Calculation
Using Euclid's algorithm
A much more efficient method is the Euclidean algorithm, which uses the division
algorithm in combination with the observation that the gcd of two numbers also divides
their difference. To compute gcd(48,18), divide 48 by 18 to get a quotient of 2 and a
remainder of 12. Then divide 18 by 12 to get a quotient of 1 and a remainder of 6. Then
divide 12 by 6 to get a remainder of 0, which means that 6 is the gcd. Note that we
ignored the quotient in each step except to notice when it reached 0, signalling that we
had arrived at the answer. Formally the algorithm can be described as:
gcd(a,0) = a
Which also could be written as
gcd(a,0) = a
gcd(a,b) = gcd(b,a mod b).
Other methods
If a and b are not both zero, the greatest common divisor of a and b can be computed by
using least common multiple (lcm) of a and b:
Properties
•
•
•
Every common divisor of a and b is a divisor of gcd(a, b).
gcd(a, b), where a and b are not both zero, may be defined alternatively and
equivalently as the smallest positive integer d which can be written in the form
d = a·p + b·q where p and q are integers. This expression is called Bézout's
identity. Numbers p and q like this can be computed with the extended Euclidean
algorithm.
gcd(a, 0) = |a|, for a ≠ 0, since any number is a divisor of 0, and the greatest
divisor of a is |a|. This is usually used as the base case in the Euclidean algorithm.
2
•
•
•
•
•
•
•
•
•
If a divides the product b·c, and gcd(a, b) = d, then a/d divides c.
If m is a non-negative integer, then gcd(m·a, m·b) = m·gcd(a, b).
If m is any integer, then gcd(a + m·b, b) = gcd(a, b).
If m is a nonzero common divisor of a and b, then gcd(a/m, b/m) = gcd(a, b)/m.
The gcd is a multiplicative function in the following sense: if a1 and a2 are
relatively prime, then gcd(a1·a2, b) = gcd(a1, b)·gcd(a2, b).
The gcd is a commutative function: gcd(a, b) = gcd(b, a).
The gcd is an associative function: gcd(a, gcd(b, c)) = gcd(gcd(a, b), c).
The gcd of three numbers can be computed as gcd(a, b, c) = gcd(gcd(a, b), c), or
in some different way by applying commutativity and associativity. This can be
extended to any number of numbers.
gcd(a, b) is closely related to the least common multiple lcm(a, b): we have
gcd(a, b)·lcm(a, b) = a·b.
This formula is often used to compute least common multiples: one first computes
the gcd with Euclid's algorithm and then divides the product of the given numbers
by their gcd.
•
The following versions of distributivity hold true:
gcd(a, lcm(b, c)) = lcm(gcd(a, b), gcd(a, c))
lcm(a, gcd(b, c)) = gcd(lcm(a, b), lcm(a, c)).
•
•
It is useful to define gcd(0, 0) = 0 and lcm(0, 0) = 0 because then the natural
numbers become a complete distributive lattice with gcd as meet and lcm as join
operation. This extension of the definition is also compatible with the
generalization for commutative rings given below.
In a Cartesian coordinate system, gcd(a, b) can be interpreted as the number of
points with integral coordinates on the straight line joining the points (0, 0) and
(a, b), excluding (0, 0).
*********************
************
Least common multiple
In arithmetic and number theory, the least common multiple (also called the lowest
common multiple or smallest common multiple) of two integers a and b, usually
denoted by LCM(a, b), is the smallest positive integer that is a multiple of both a and
b.[1] It is familiar from grade-school arithmetic as the "lowest common denominator" that
must be determined before two fractions can be added.
If either a or b is 0, LCM(a, b) is defined to be zero.
3
The LCM of more than two integers is also well-defined: it is the smallest number that is
an integer multiple of each of them.
Example
What is the LCM of 4 and 6?
Multiples of 4 are:
4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76 etc.
and the multiples of 6 are:
6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, ...
Common multiples of 4 and 6 are simply the numbers that are in both lists:
12, 24, 36, 48, 60, 72, ....
So the least common multiple of 4 and 6 is the smallest one of those: 12
Applications
When adding, subtracting, or comparing vulgar fractions, it is useful to find the least
common multiple of the denominators, often called the lowest common denominator,
because each of the fractions can be expressed as a fraction with this denominator. For
instance,
where the denominator 42 was used because it is the least common multiple of 21 and 6.
Computing the least common multiple
Reduction by the greatest common divisor
The following formula reduces the problem of computing the least common multiple to
the problem of computing the greatest common divisor (GCD):
4
This formula is also valid when exactly one of a and b is 0, since gcd(a, 0) = |a|.
There are fast algorithms for computing the GCD that do not require the numbers to be
factored, such as the Euclidean algorithm. To return to the example above,
Because gcd(a, b) is a divisor of both a and b, it's more efficient to compute the LCM by
dividing before multiplying:
This reduces the size of one input for both the division and the multiplication, and
reduces the required storage needed for intermediate results (overflow in the a×b
computation). Because gcd(a, b) is a divisor of both a and b, and thus the division will be
guaranteed to yield an integer, so the intermediate result can be stored in an integer. Done
this way, the previous example becomes:
Finding least common multiples by prime factorization
The unique factorization theorem says that every positive integer greater than 1 can be
written in only one way as a product of prime numbers. The prime numbers can be
considered as the atomic elements which, when combined together, make up a composite
number.
For example:
Here we have the composite number 90 made up of one atom of the prime number 2, two
atoms of the prime number 3 and one atom of the prime number 5.
This knowledge can be used to find the lcm of a set of numbers.
Example: Find the value of lcm(8,9,21).
First, factor out each number and express it as a product of prime number powers.
5
The lcm will be the product of multiplying the highest power in each prime factor
category together. Out of the 4 prime factor categories 2, 3, 5, and 7, the highest powers
from each are 23, 32, 50, and 71. Thus,
This method is not as efficient as reducing to the greatest common divisor, since there is
no known general efficient algorithm for integer factorization, but is useful in illustrating
concepts.
A simple algorithm
This method works as easily for finding the LCM of several integers.
Let there be a finite sequence of positive integers X = (x1, x2, ..., xn), n > 1. The algorithm
proceeds in steps as follows: on each step m it examines and updates the sequence X(m) =
(x1(m), x2(m), ..., xn(m)), X(1) = X. The purpose of the examination is to pick up the least
(perhaps, one of many) element of the sequence X(m). Assuming xk0(m) is the selected
element, the sequence X(m+1) is defined as
xk(m+1) = xk(m), k ≠ k0
xk0(m+1) = xk0(m) + xk0.
In other words, the least element is increased by the corresponding x whereas the rest of
the elements pass from X(m) to X(m+1) unchanged.
The algorithm stops when all elements in sequence X(m) are equal. Their common value L
is exactly LCM(X). (For a proof and an interactive simulation see reference below,
Algorithm for Computing the LCM.)
**************************
**********
6
Download