ArticleslgStudy

computer science

Binary GCD algorithm

Binary GCD algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Binary GCD algorithm rather than just read about it. In short: The binary GCD algorithm, also known as Stein's algorithm or the binary Euclidean algorithm, is an algorithm that computes the greatest common divisor (GCD) of two nonnegative integers. Stein's algorithm uses simpler arithmetic operations than the conventional Euclidean algorithm; it replaces division with arithmetic shifts, comparisons, and subtraction.

Binary GCD algorithm — main illustration
Binary GCD algorithm — illustration

Key takeaways

  • Binary GCD algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Binary GCD algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Binary GCD algorithm from memory before moving on to harder problems.

Reference excerpt

The binary GCD algorithm, also known as Stein's algorithm or the binary Euclidean algorithm, is an algorithm that computes the greatest common divisor (GCD) of two nonnegative integers. Stein's algorithm uses simpler arithmetic operations than the conventional Euclidean algorithm; it replaces division with arithmetic shifts, comparisons, and subtraction. Although the algorithm in its contemporary form was first published by the physicist and programmer Josef Stein in 1967, it was known by the 2nd century BCE, in ancient China.

Algorithm The algorithm finds the GCD of two nonnegative numbers u and v by repeatedly applying these identities:

gcd⁡(u,0)=u: everything divides zero, and u is the largest number that divides u. gcd⁡(2u,2v)=2⋅gcd⁡(u,v): 2 is a common divisor. gcd⁡(u,2v)=gcd⁡(u,v) if u is odd: 2 is then not a common divisor. gcd⁡(u,v)=gcd⁡(u,v−u) if u,v odd and u≤v. As GCD is commutative (gcd⁡(u,v)=gcd⁡(v,u)), those identities still apply if the operands are swapped: gcd⁡(0,v)=v, gcd⁡(2u,v)=gcd⁡(u,v) if v is odd, etc.

Implementation While the above description of the algorithm is mathematically correct, the binary GCD algorithm performs more loop iterations that the Euclidean one, so it only offers a performance advantage if those iterations are substantially faster. Performant software implementations typically differ from it in a few notable ways:

eschewing repeated trial division by 2 in favour of the count trailing zeros primitive and a single bit shift, which is functionally equivalent to repeatedly applying identity 3, but much faster; expressing the algorithm iteratively rather than recursively: the resulting implementation can be laid out to avoid repeated work, invoking identity 2 at the start and maintaining as invariant that both numbers are odd upon entering the loop, which only needs to implement identities 3 and 4; and making the loop's body branch-free except for its exit condition v == 0: unpredictable branches can have a large, negative impact on performance. The following is an implementation of the algorithm in C.

The following is an implementation of the algorithm in Rust exemplifying those differences, adapted from uutils. The conditional exchange of u and v (ensuring u≤v) compiles down to conditional moves;:

Note: The implementation above accepts unsigned (non-negative) integers; given that gcd⁡(u,v)=gcd⁡(±u,±v), the signed case can be handled as follows:

Complexity Asymptotically, the algorithm requires O(n) steps, where n is the number of bits in the larger of the two numbers, as every two steps reduce at least one of the operands by at least a factor of 2. Each step involves only a few arithmetic operations (O(1) with a small constant); when working with word-sized numbers, each arithmetic operation translates to a single machine operation, so the number of machine operations is on the order of n, i.e. log2(max⁡(u,v)). For arbitrarily large numbers, the asymptotic complexity of this algorithm is O(n2), as each arithmetic operation (subtract and shift) involves a linear number of machine operations (one per word in the numbers' binary representation). If the numbers can be represented in the machine's memory, i.e. each number's size can be represented by a single machine word, this bound is reduced to: O(n2log2n) This is the same as for the Euclidean algorithm, though a more precise analysis by Akhavi and Vallée proved that binary GCD uses about 60% fewer bit operations.

Extensions The binary GCD algorithm can be extended in several ways, either to output additional information, deal with arbitrarily large integers more efficiently, or to compute GCDs in domains other than the integers. The extended binary GCD algorithm, analogous to the extended Euclidean algorithm, fits in the first kind of extension, as it provides the Bézout coefficients in addition to the GCD: integers a and b such that a⋅u+b⋅v=gcd⁡(u,v). In the case of large integers, the best asymptotic complexity is O(M(n)log⁡n), with M(n) the cost of n-bit multiplication; this is near-linear and vastly smaller than the binary GCD algorithm's O(n2), though concrete implementations only outperform older algorithms for numbers larger than about 64 kilobits (i.e. greater than 8×1019265). This is achieved by extending the binary GCD algorithm using ideas from the Schönhage–Strassen algorithm for fast integer multiplication. The binary GCD algorithm has also been extended to domains other than natural numbers, such as Gaussian integers, Eisenstein integers, quadratic rings, and integer rings of number fields.

Historical description An algorithm for computing the GCD of two numbers was known in ancient China, under the Han dynasty, as a method to reduce fractions:

If possible halve it; otherwise, take the denominator and the numerator, subtract the lesser from the greater, and do that alternately to make them the same. Reduce by the same number. The phrase "if possible halve it" is ambiguous,

if this applies when either of the numbers become even, the algorithm is the binary GCD algorithm; if this only applies when both numbers are even, the algorithm is similar to the Euclidean algorithm.

See also

Euclidean algorithm Extended Euclidean algorithm Least common multiple

References

Further reading Knuth, Donald (1998). "§4.5 Rational arithmetic". Seminumerical Algorithms. The Art of Computer Programming. Vol. 2 (3rd ed.). Addison-Wesley. pp. 330–417. ISBN 978-0-201-89684-8. Covers the extended binary GCD, and a probabilistic analysis of the algorithm.

Cohen, Henri (1993). "Chapter 1 : Fundamental Number-Theoretic Algorithms". A Course in Computational Algebraic Number Theory. Graduate Texts in Mathematics. Vol. 138. Springer-Verlag. pp. 12–24. ISBN 0-387-55640-0. Covers a variety of topics, including the extended binary GCD algorithm which outputs Bézout coefficients, efficient handling of multi-precision integers using a variant of Lehmer's GCD algorithm, and the relationship between GCD and continued fraction expansions of real numbers.

… excerpt ends here. Continue reading the full article.

Illustrations

Binary GCD algorithm: Visualisation of using the binary GCD algorithm to find the greatest common divisor (GCD) of 36 and 24.  Thus, the GCD is 22 × 3 = 12.
Visualisation of using the binary GCD algorithm to find the greatest common divisor (GCD) of 36 and 24. Thus, the GCD is 22 × 3 = 12.

Worked examples

Example 1 — a first encounter with Binary GCD algorithm

Start with the simplest possible case. Write down what Binary GCD algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Binary GCD algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Binary GCD algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Binary GCD algorithm

In research
Binary GCD algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Binary GCD algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Binary GCD algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Number theoretic algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Binary GCD algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Binary GCD algorithm” →

How to study Binary GCD algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Binary GCD algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Binary GCD algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Binary GCD algorithm in simple terms?

The binary GCD algorithm, also known as Stein's algorithm or the binary Euclidean algorithm, is an algorithm that computes the greatest common divisor (GCD) of two nonnegative integers. Stein's algorithm uses simpler arithmetic operations than the conventional Euclidean algorithm; it replaces divis…

Why does Binary GCD algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Binary GCD algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Binary GCD algorithm.

Tags

  • Number theoretic algorithms

Keep exploring