Wednesday, July 31, 2019

Greatest common denominator

Script

#!/usr/local/bin/python3.7

def gcd(n1, n2):
    minimum = n1 if n1 < n2 else n2
    largest_factor = 1
    for x in range(1, minimum + 1):
        if n1 % x == 0 and n2 % x ==0:
            largest_factor = x
    return largest_factor

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

minimum = num1 if num1 < num2 else num2

print(gcd(num1, num2))
print()


Execution

Enter the first number: 68
Enter the second number: 51
17


Enter the first number: 189
Enter the second number: 105
21






No comments:

Post a Comment