0% found this document useful (0 votes)
58 views4 pages

Computing GCD's The Euclidean Algorithm: A QB + R

The document discusses the GCD Remainder Lemma and its use in the Euclidean algorithm to efficiently calculate the greatest common divisor (GCD) of two numbers. The GCD Remainder Lemma states that the GCD of two numbers is equal to the GCD of one number and the remainder of dividing the other by it. The Euclidean algorithm applies this lemma iteratively by replacing one number with the remainder until one number is zero, at which point the other number is the GCD. The algorithm is proven to terminate in O(log b) steps by showing that one number is halved or reduced at every other step.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

Computing GCD's The Euclidean Algorithm: A QB + R

The document discusses the GCD Remainder Lemma and its use in the Euclidean algorithm to efficiently calculate the greatest common divisor (GCD) of two numbers. The GCD Remainder Lemma states that the GCD of two numbers is equal to the GCD of one number and the remainder of dividing the other by it. The Euclidean algorithm applies this lemma iteratively by replacing one number with the remainder until one number is zero, at which point the other number is the GCD. The algorithm is proven to terminate in O(log b) steps by showing that one number is halved or reduced at every other step.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mathematics for Computer Science GCD Remainder Lemma

MIT 6.042J/18.062J
Lemma:

Computing GCD’s gcd(a,b) = gcd(b, rem(a,b))


for b ≠ 0
The Euclidean Proof: a = qb + r
Algorithm any divisor of 2 of these
terms must divide all 3.
Albert R Meyer March 6, 2015 gcdeuclid.1 Albert R Meyer March 6, 2015 gcdeuclid.2

GCD Remainder Lemma GCD example


Lemma:
Example: a = 899, b=493
gcd(a,b) = gcd(b, rem(a,b)) GCD(899, 493) =
for b ≠ 0 GCD(493, 406) =
GCD(406, 87) =
Proof: a = qb + r
GCD(87, 58) =
so a,b and b,r have GCD(58, 29) =
the same divisors GCD(29, 0) = 29
Albert R Meyer March 6, 2015 gcdeuclid.3 Albert R Meyer March 6, 2015 gcdeuclid.4

1
Euclidean Algorithm GCD partial correctness
as a State Machine: By Lemma, gcd(x,y) is constant.
States ::= N × N so preserved invariant is
start ::= (a,b) P((x,y)) ::= [gcd(a,b) = gcd(x,y)]
state transitions defined by P(start) is trivially true:
(x,y) → (y, rem(x,y)) [gcd(a,b) = gcd(a,b)]
for y ≠ 0
Albert R Meyer March 6, 2015 gcdeuclid.5 Albert R Meyer March 6, 2015 gcdeuclid.6

GCD partial correctness GCD Termination


at termination (if any) At each transition, x is replaced
x = gcd(a,b) by y.

Proof: at termination, y = 0, so
x = gcd(x,0) = gcd(x,y) = gcd(a,b)
preserved invariant

Albert R Meyer March 6, 2015 gcdeuclid.7 Albert R Meyer March 6, 2015 gcdeuclid.8

2
GCD Termination GCD Termination

At each transition, x is replaced At each transition, x is replaced


by y. If y < x/2, then x gets by y. If y < x/2, then x gets
halved at this step. halved at this step. If y > x/2,
then rem(x,y) = x – y < x/2,
so y gets halved when it is
replaced by rem(x,y) after the
next step.
Albert R Meyer March 6, 2015 gcdeuclid.9 Albert R Meyer March 6, 2015 gcdeuclid.10

GCD Termination

y halves or smaller at
every other step, so
reaches minimum in ≤
2 log2 b
steps.

Albert R Meyer March 6, 2015 gcdeuclid.11

3
MIT OpenCourseWare
https://ocw.mit.edu

6.042J / 18.062J Mathematics for Computer Science


Spring 2015

For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy