Information about Euclidean Algorithm
Not to be confused with Euclidean geometry.
In number theory, the Euclidean algorithm (also called Euclid's algorithm) is an algorithm to determine the greatest common divisor (GCD) of two elements of any Euclidean domain (for example, the integers). Its major significance is that it does not require factoring the two integers, and it is also significant in that it is one of the oldest algorithms known, dating back to the ancient Greeks.
History of the Euclidean algorithm
The Euclidean algorithm is one of the oldest algorithms known, since it appeared in Euclid's Elements around 300 BC. Euclid originally formulated the problem geometrically, as the problem of finding a common "measure" for two line lengths, and his algorithm proceeded by repeated subtraction of the shorter from the longer segment. However, the algorithm was probably not discovered by Euclid and it may have been known up to 200 years earlier. It was almost certainly known by Eudoxus of Cnidus (about 375 BC), and Aristotle (about 330 BC) hinted at it in his Topics, 158b, 29-35.Description of the algorithm
Given two natural numbers a and b, not both equal to zero: check if b is zero; if yes, a is the gcd. If not, repeat the process using, respectively, b, and the remainder after dividing a by b. The remainder after dividing a by b is usually written as a mod b.These algorithms can be used in any context where division with remainder is possible. This includes rings of polynomials over a field as well as the ring of Gaussian integers, and in general all Euclidean domains. Applying the algorithm to the more general case other than natural number will be discussed in more detail later in the article.
Using recursion
Using recursion, the algorithm can be expressed: function gcd(a, b) if b = 0 return a else return gcd(b, a mod b)Using iteration
An efficient, iterative method, for compilers that don't optimize tail recursion:function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a
The extended Euclidean algorithm
Original algorithm
The original algorithm as described by Euclid treated the problem geometrically, using repeated subtraction rather than mod (remainder).function gcd(a, b) while b ≠ 0 if a > b a := a - b else b := b - a return a
Notice that while b can be zero, a cannot be zero; otherwise, this becomes an infinite loop. This algorithm does not require an extra variable t.
An example
As an example, consider computing the gcd of 1071 and 1029, which is 21. Recall that “mod” means “the remainder after dividing.”With the recursive algorithm:
| a | b | Explanations | ||
|---|---|---|---|---|
| gcd( | 1071, | 1029) | The initial arguments | |
| = | gcd( | 1029, | 42) | The second argument is 1071 mod 1029 |
| = | gcd( | 42, | 21) | The second argument is 1029 mod 42 |
| = | gcd( | 21, | 0) | The second argument is 42 mod 21 |
| = | 21 | Since b=0, we return a |
With the iterative algorithm:
| a | b | Explanation |
|---|---|---|
| 1071 | 1029 | Step 1: The initial inputs |
| 1029 | 42 | Step 2: The remainder of 1071 divided by 1029 is 42, which is put on the right, and the divisor 1029 is put on the left. |
| 42 | 21 | Step 3: We repeat the loop, dividing 1029 by 42, and get 21 as remainder. |
| 21 | 0 | Step 4: Repeat the loop again, since 42 is divisible by 21, we get 0 as remainder, and the algorithm terminates. The number on the left, that is 21, is the gcd as required. |
Observe that a ≥ b in each call. If initially, b > a, there is no problem; the first iteration effectively swaps the two values.
Proof
Suppose a and b are the natural numbers whose gcd has to be determined. And suppose the remainder of the division of a by b is r. Therefore a = qb + r where q is the quotient of the division.Any common divisor of a and b is also a divisor of r. To see why this is true, consider that r can be written as r = a − qb. Now, if there is a common divisor d of a and b such that a = sd and b = td, then r = (s−qt)d. Since all these numbers, including s−qt, are whole numbers, it can be seen that r is divisible by d.
The above analysis is true for any divisor d; thus, the greatest common divisor of a and b is also the greatest common divisor of b and r. Therefore it is enough if we continue searching for the greatest common divisor with the numbers b and r. Since r is smaller in absolute value than b, we will reach r = 0 after finitely many steps.
Running time
When analyzing the running time of Euclid's algorithm, it turns out that the inputs requiring the most divisions are two successive Fibonacci numbers (because their ratios are the convergents in the slowest continued fraction expansion to converge, that of the golden ratio) as proved by Gabriel Lamé, and the worst case requires O(n) divisions, where n is the number of digits in the input. However, the divisions themselves are not constant time operations; the actual time complexity of the algorithm is
.
The reason is that division of two n-bit numbers takes time
, where m is the length of the quotient. Consider the computation of gcd(a,b) where a and b have at most n bits, let
be the sequence of numbers produced by the algorithm, and let
be their lengths. Then
, and the running time is bounded by
This is considerably better than Euclid's original algorithm, in which the modulus operation is effectively performed using repeated subtraction in
steps. Consequently, that version of the algorithm requires
time for n-digit numbers, or
time for the number m.
Euclid's algorithm is widely used in practice, especially for small numbers, due to its simplicity. An alternative algorithm, the binary GCD algorithm, exploits the binary representation used by computers to avoid divisions and thereby increase efficiency, although it too is O(n²); it merely shrinks the constant hidden by the big-O notation on many real machines.
There are more complex algorithms that can reduce the running time to
. See Computational complexity of mathematical operations for more details.
Relation with continued fractions
The quotients that appear when the Euclidean algorithm is applied to the inputs a and b are precisely the numbers occurring in the continued fraction representation of a/b. Take for instance the example of a = 1071 and b = 1029 used above. Here is the calculation with highlighted quotients:- 1071 = 1029 × 1 + 42
- 1029 = 42 × 24 + 21
- 42 = 21 × 2 + 0
Consequently,
.
The quotients 1,24,2 count certain squares nested within a rectangle R having length 1071 and width 1029, in the following manner:
(1) there is 1 1029×1029 square in R whose removal leaves a 42×1029 rectangle, R1;
(2) there are 24 42×42 squares in R1 whose removal leaves a 21×42 rectangle, R2;
(3) there are 2 21×21 squares in R2 whose removal leaves nothing.
The "visual Euclidean algorithm" of nested squares applies to an arbitrary rectangle R. If the (length)/(width) of R is an irrational number, then the visual Euclidean algorithm extends to a visual continued fraction.
Generalization to Euclidean domains
The Euclidean algorithm can be applied to some rings, not just the integers. The most general context in which the algorithm terminates with the greatest common divisor is in a Euclidean domain. For instance, the Gaussian integers and polynomial rings over a field are both Euclidean domains.As an example, consider the ring of polynomials with rational coefficients. In this ring, division with remainder is carried out using long division, also known as synthetic division. The resulting polynomials are then made monic by factoring out the leading coefficient.
We calculate the greatest common divisor of
and
Following the algorithm gives these values:
| a | b |
|---|---|
![]() | ![]() |
![]() | ![]() |
![]() | ![]() |
![]() | ![]() |
This agrees with the explicit factorization. For general Euclidean domains, the proof of correctness is by induction on some size function. For the integers, this size function is just the identity. For rings of polynomials over a field, it is the degree of the polynomial (note that each step in the above table reduces the degree by one).
See also
References
- Donald Knuth. The Art of Computer Programming, Volume 2: Seminumerical Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89684-2. Sections 4.5.2–4.5.3, pp.333–379.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 31.2: Greatest common divisor, pp.856–862.
- Clark Kimberling. "A Visual Euclidean Algorithm," Mathematics Teacher 76 (1983) 108-109.
External links
- Euclid's Algorithm at cut-the-knot
- Binary Euclid's Algorithm (Java) at cut-the-knot
- Euclid's Game (Java) at cut-the-knot
- Eric W. Weisstein, Euclidean Algorithm at MathWorld.
- Eric W. Weisstein, Lamé's Theorem at MathWorld.
- Euclid's algorithm at PlanetMath.
- Music and Euclid's algorithm
- Implementation in Javascript
- .NET Implementation of Euclidean algorithm
Euclidean geometry is a mathematical system attributed to the Greek mathematician Euclid of Alexandria. Euclid's text Elements is the earliest known systematic discussion of geometry.
..... Click the link for more information.
..... Click the link for more information.
Number theory is the branch of pure mathematics concerned with the properties of numbers in general, and integers in particular, as well as the wider classes of problems that arise from their study.
..... Click the link for more information.
..... Click the link for more information.
In mathematics, computing, linguistics, and related disciplines, an algorithm is a finite list of well-defined instructions for accomplishing some task that, given an initial state, will proceed through a well-defined series of successive states, eventually terminating in an
..... Click the link for more information.
..... Click the link for more information.
In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers without remainder.
..... Click the link for more information.
..... Click the link for more information.
In abstract algebra, a Euclidean domain (also called a Euclidean ring) is a type of ring in which the Euclidean algorithm can be used.
..... Click the link for more information.
Definition
Formally, a Euclidean domain is an integral domain D on which one can define a function v..... Click the link for more information.
The integers (from the Latin integer, which means with untouched integrity, whole, entire) are the set of numbers including the whole numbers (0, 1, 2, 3, …) and their negatives (0, −1, −2, −3, …).
..... Click the link for more information.
..... Click the link for more information.
factorization (British English: also factorisation) or factoring is the decomposition of an object (for example, a number, a polynomial, or a matrix) into a product of other objects, or factors, which when multiplied together give the original.
..... Click the link for more information.
..... Click the link for more information.
The term ancient Greece refers to the periods of Greek history in Classical Antiquity, lasting ca. 750 BC[1] (the archaic period) to 146 BC (the Roman conquest). It is generally considered to be the seminal culture which provided the foundation of Western Civilization.
..... Click the link for more information.
..... Click the link for more information.
Euclid's Elements (Greek: Στοιχεῖα) is a mathematical and geometric treatise consisting of 13 books written by the Greek mathematician Euclid in Alexandria circa 300 BC.
..... Click the link for more information.
..... Click the link for more information.
3rd century BC - 2nd century BC
330s BC 320s BC 310s BC - 300s BC - 290s BC 280s BC 270s BC
303 BC 302 BC 301 BC - 300 BC - 299 BC 298 BC 297 BC
Politics
State leaders - Sovereign states
..... Click the link for more information.
330s BC 320s BC 310s BC - 300s BC - 290s BC 280s BC 270s BC
303 BC 302 BC 301 BC - 300 BC - 299 BC 298 BC 297 BC
Politics
State leaders - Sovereign states
..... Click the link for more information.
Euclid
Born fl. 300 BC
Residence Alexandria, Egypt
Nationality Greek
Field Mathematics
Known for Euclid's Elements Euclid (Greek:
..... Click the link for more information.
Born fl. 300 BC
Residence Alexandria, Egypt
Nationality Greek
Field Mathematics
Known for Euclid's Elements Euclid (Greek:
..... Click the link for more information.
Another article concerns Eudoxus of Cyzicus.
Eudoxus of Cnidus (Greek Εύδοξος) (410 or 408 BC – 355 or 347 BC) was a Greek astronomer, mathematician, physician, scholar and student of Plato.
..... Click the link for more information.
Aristotle (Greek: Ἀριστοτέλης Aristotélēs) (384 BC – 322 BC) was a Greek philosopher, a student of Plato and teacher of Alexander the Great.
..... Click the link for more information.
..... Click the link for more information.
In mathematics, a natural number can mean either an element of the set (i.e the positive integers or the counting numbers) or an element of the set (i.e. the non-negative integers).
..... Click the link for more information.
..... Click the link for more information.
modulo operation finds the remainder of division of one number by another.
Given two numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder, on division of a
..... Click the link for more information.
Given two numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder, on division of a
..... Click the link for more information.
In abstract algebra, a polynomial ring is the set of polynomials in one or more variables with coefficients in a ring.
..... Click the link for more information.
Definition of a polynomial
In real analysis, a polynomial is a certain type of a function of one or several variables (see polynomial), or in other words,..... Click the link for more information.
field is an algebraic structure in which the operations of addition, subtraction, multiplication and division (except division by zero) may be performed, and the same rules hold which are familiar from the arithmetic of ordinary numbers.
..... Click the link for more information.
..... Click the link for more information.
A Gaussian integer is a complex number whose real and imaginary part are both integers. The Gaussian integers, with ordinary addition and multiplication of complex numbers, form an integral domain, usually written as Z[i].
..... Click the link for more information.
..... Click the link for more information.
In abstract algebra, a Euclidean domain (also called a Euclidean ring) is a type of ring in which the Euclidean algorithm can be used.
..... Click the link for more information.
Definition
Formally, a Euclidean domain is an integral domain D on which one can define a function v..... Click the link for more information.
Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition. The term is also used more generally to describe a process of repeating objects in a self-similar way.
..... Click the link for more information.
..... Click the link for more information.
In computer science, tail recursion (or tail-end recursion) is a special case of recursion in which the last operation of the function is a recursive call. Such recursions can be easily transformed to iterations.
..... Click the link for more information.
..... Click the link for more information.
The extended Euclidean algorithm is an extension to the Euclidean algorithm for finding the greatest common divisor (GCD) of integers a and b: it also finds the integers x and y in Bézout's identity
..... Click the link for more information.
..... Click the link for more information.
In mathematics, the absolute value (or modulus[1]) of a real number is its numerical value without regard to its sign. So, for example, 3 is the absolute value of both 3 and −3.
..... Click the link for more information.
..... Click the link for more information.
Fibonacci numbers form a sequence defined by the following recurrence relation:
..... Click the link for more information.
..... Click the link for more information.
A convergent is one of a sequence of values obtained by evaluating successive truncations of a continued fraction. The nth convergent is also known as the nth approximant of a continued fraction.
..... Click the link for more information.
..... Click the link for more information.
In mathematics, a continued fraction is an expression such as
where a0 is some integer and all the other numbers an are positive integers. Longer expressions are defined analogously.
..... Click the link for more information.
where a0 is some integer and all the other numbers an are positive integers. Longer expressions are defined analogously.
..... Click the link for more information.
golden section is a line segment sectioned into two according to the golden ratio. The total length a+b is to the longer segment a as a
..... Click the link for more information.
..... Click the link for more information.
Gabriel Lamé (July 22, 1795 - May 1, 1870) was a French mathematician.
..... Click the link for more information.
Biography
Lamé was born in Tours, in today's département of Indre-et-Loire...... Click the link for more information.
In computational complexity theory, big O notation is often used to describe how the size of the input data affects an algorithm's usage of computational resources (usually running time or memory).
..... Click the link for more information.
..... Click the link for more information.
The binary GCD algorithm is an algorithm which computes the greatest common divisor of two nonnegative integers. It gains a measure of efficiency over the ancient Euclidean algorithm by replacing divisions and multiplications with shifts, which are cheaper when operating on the
..... Click the link for more information.
..... Click the link for more information.
This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus







