Jak znaleźć GCD dwóch liczb w Pythonie
import math
# Using the built-in gcd method
gcd_built_in = math.gcd(10, 5)
print("GCD:", gcd_built_in) # GCD: 5
# Creating a new gcd method
# implementing Euclid's algorithm
def gcd_euclid(num1, num2):
if(num1 == 0):
return num2
if(num2 == 0):
return num1
while(num1 != num2):
if(num1 > num2):
num1 = num1-num2
else:
num2 = num2 - num1
return num1
gcd_euclid = gcd_euclid(3, 7)
print("GCD:", gcd_euclid) # GCD: 1
Wissam