Sylvester
0
Q:

python code for gcd of two numbers

# Python code to demonstrate naive 
# method to compute gcd ( recursion ) 
 def hcfnaive(a,b): 
    if(b==0): 
        return a 
    else: 
        return hcfnaive(b,a%b)   
a = 60
b= 48  
# prints 12 
print ("The gcd of 60 and 48 is : ",end="") 
print (hcfnaive(60,48)) 

# Python code to demonstrate naive 
# method to compute gcd ( Loops ) 
def computeGCD(x, y): 
	if x > y: 
		small = y 
	else: 
		small = x 
	for i in range(1, small+1): 
		if((x % i == 0) and (y % i == 0)): 
			gcd = i 			
	return gcd 
a = 60
b= 48
# prints 12 
print ("The gcd of 60 and 48 is : ",end="") 
print (computeGCD(60,48)) 

# Python code to demonstrate naive 
# method to compute gcd ( Euclidean algo ) 
def computeGCD(x, y): 
while(y): 
	x, y = y, x % y 
return x 

a = 60
b= 48
# prints 12 
print ("The gcd of 60 and 48 is : ",end="") 
print (computeGCD(60,48)) 
2
# Function to find HCF the Using Euclidian algorithm
def compute_hcf(x, y):
   while(y):
       x, y = y, x % y
   return x

hcf = compute_hcf(300, 400)
print("The HCF is", hcf)
0

New to Communities?

Join the community