Python Recap - I: Madhavan Mukund
Python Recap - I: Madhavan Mukund
Python Recap - I: Madhavan Mukund
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Computing gcd(m, n)
gcd(m, n) ≤ min(m, n)
Compute list of common factors from
1 to min(m, n)
Return the last such common factor
Madhavan Mukund Python Recap – I PDSA using Python Week 1 2/4
Computing gcd
Points to note
def gcd(m,n):
Need to initialize cf for cf.append()
cf = [] # List of common factors
to work
for i in range(1,min(m,n)+1):
Variables (names) derive their type if (m%i) == 0 and (n%i) == 0:
from the value they hold cf.append(i)
return(cf[-1])
Points to note
def gcd(m,n):
Need to initialize cf for cf.append()
cf = [] # List of common factors
to work
for i in range(1,min(m,n)+1):
Variables (names) derive their type if (m%i) == 0 and (n%i) == 0:
from the value they hold cf.append(i)
Control flow return(cf[-1])
Conditionals (if)
Loops (for)
Points to note
def gcd(m,n):
Need to initialize cf for cf.append()
cf = [] # List of common factors
to work
for i in range(1,min(m,n)+1):
Variables (names) derive their type if (m%i) == 0 and (n%i) == 0:
from the value they hold cf.append(i)
Control flow return(cf[-1])
Conditionals (if)
Loops (for)
Points to note
def gcd(m,n):
Need to initialize cf for cf.append()
cf = [] # List of common factors
to work
for i in range(1,min(m,n)+1):
Variables (names) derive their type if (m%i) == 0 and (n%i) == 0:
from the value they hold cf.append(i)
Control flow return(cf[-1])
Conditionals (if)
Loops (for)