# 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)

### 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])

### Greatest common divisor program

def gcd(m,n):

fm = []

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

if (m%i) == 0:

fm.append(i)

fn = []

for j in range(1,n+1):

if (n%j) == 0:

fn.append(j)

cf = []

for f in fm:

if f in fn:

cf.append(f)

return(cf[-1])

Subscribe to:
Posts (Atom)