Nptel Mooc: Programming, Data Structures and Algorithms in Python
Nptel Mooc: Programming, Data Structures and Algorithms in Python
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 1, Lecture 3
Then m = ad, n = bd
So m-n = ad - bd = (a-b)d
So gcd(m,n) = gcd(n,m-n)
Euclid’s algorithm
If n divides m, return n
return(n)
Even better
Suppose n does not divide m
Then m = ad, n = bd
So ad = q(bd) + r
If n divides m, return n
Return gcd(n,r)
Euclid’s algorithm
def gcd(m,n):
def gcd(m,n):