# Credits to NPTEL MOOC,Programming, Data Structures & Algorithms

#in
Python by Madhavan Mukund, Chennai Mathematical Institute

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)

Solve Problems by Coding Solutions - A Complete solution for python programming

### Greatest common divisor program using euclids algorithms

#Credits to NPTEL MOOC, Programming, Data Structures & Algorithms in #Python by Madhavan Mukund, Chennai Mathematical Institute

def gcd(m,n):

if m < n:

(m,n) = (n,m)

if (m%n) == 0:

return(n)

else:

diff = m-n

return(gcd(max(n,diff),min(n,diff)))

print(gcd(12,3))

### Greatest common divisor program with looping

#Credits to NPTEL MOOC, Programming, Data Structures & Algorithms in #Python by Madhavan Mukund, Chennai Mathematical Institute

def gcd(m,n):

i = min(m,n)

while i > 0:

if (m%i) == 0 and (n%i) == 0:

return(i)

else:

i = i-1

### Greatest common divisor program without list Implementation

#Credits to NPTEL MOOC, Programming, Data Structures & Algorithms in #Python by Madhavan Mukund, Chennai Mathematical Institute

def gcd(m,n):

for i in range(1,min(m,n)+1):

if (m%i) == 0 and (n%i) == 0:

mrcf = i

return(mrcf)

### Greatest common divisor program - A shorter Implementation

#Credits to NPTEL MOOC, Programming, Data Structures & Algorithms #in
Python by Madhavan Mukund, Chennai Mathematical Institute

def gcd(m,n):

cf = []

for i in range(1,min(m,n)+1):

if (m%i) == 0 and (n%i) == 0:

cf.append(i)

return(cf[-1])

def gcd(m,n):

cf = []

for i in range(1,min(m,n)+1):

if (m%i) == 0 and (n%i) == 0:

cf.append(i)

return(cf[-1])

Subscribe to:
Posts (Atom)