0% found this document useful (0 votes)
23 views

Python Week1 Lecture3 Handout

The document discusses the Euclid's algorithm for finding the greatest common divisor (GCD) of two numbers. It explains that the algorithm works by recursively finding the GCD of the smaller number and the remainder of dividing the larger number by the smaller number, until the remainder is zero. The document provides pseudocode for an implementation of the algorithm and analyzes its time efficiency, noting that it takes time proportional to the number of digits in the larger number rather than the values themselves.

Uploaded by

Aakanksha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Python Week1 Lecture3 Handout

The document discusses the Euclid's algorithm for finding the greatest common divisor (GCD) of two numbers. It explains that the algorithm works by recursively finding the GCD of the smaller number and the remainder of dividing the larger number by the smaller number, until the remainder is zero. The document provides pseudocode for an implementation of the algorithm and analyzes its time efficiency, noting that it takes time proportional to the number of digits in the larger number rather than the values themselves.

Uploaded by

Aakanksha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

NPTEL MOOC

PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 1, Lecture 3

Madhavan Mukund, Chennai Mathematical Institute


http://www.cmi.ac.in/~madhavan
Algorithm for gcd(m,n)

To find the largest common factor, start at the end


and work backwards

Let i run from min(m,n) to 1

First common factor that we find will be gcd!


Euclids algorithm

Suppose d divides both m and n, and m > n

Then m = ad, n = bd

So m-n = ad - bd = (a-b)d

d divides m-n as well!

So gcd(m,n) = gcd(n,m-n)
Euclids algorithm

Consider gcd(m,n) with m > n

If n divides m, return n

Otherwise, compute gcd(n,m-n) and return that


value
Euclids algorithm
def gcd(m,n):
# Assume m >= n
if m < n:
(m,n) = (n,m)

if (m%n) == 0:
return(n)
else:
diff = m-n
# diff > n? Possible!
return(gcd(max(n,diff),min(n,diff))
Euclids algorithm, again
def gcd(m,n):

if m < n: # Assume m >= n


(m,n) = (n,m)

while (m%n) != 0:
diff = m-n
# diff > n? Possible!
(m,n) = (max(n,diff),min(n,diff))

return(n)
Even better
Suppose n does not divide m

Then m = qn + r, where q is the quotient, r is the


remainder when we divide m by n

Assume d divides both m and n

Then m = ad, n = bd

So ad = q(bd) + r

It follows that r = cd, so d divides r as well


Euclids algorithm

Consider gcd(m,n) with m > n

If n divides m, return n

Otherwise, let r = m%n

Return gcd(n,r)
Euclids algorithm
def gcd(m,n):

if m < n: # Assume m >= n


(m,n) = (n,m)

if (m%n) == 0:
return(n)
else:
return(gcd(n,m%n)) # m%n < n, always!
Euclids algorithm, revisited

def gcd(m,n):

if m < n: # Assume m >= n


(m,n) = (n,m)

while (m%n) != 0:
(m,n) = (n,m%n) # m%n < n, always!

return(n)
Efficiency

Can show that the second version of Euclids


algorithm takes time proportional to the number of
digits in m

If m is 1 billion (109), the naive algorithm takes


billions of steps, but this algorithm takes tens of
steps

You might also like