find the greatest common divisor of two numbers in python

One way to find the greatest common divisor (gcd) of two numbers in Python is to use the math.gcd() function from the math module. The math.gcd() function takes two arguments and returns their gcd.

Here's an example:

main.py
import math

num1 = 48
num2 = 60

gcd = math.gcd(num1, num2)

print("The gcd of", num1, "and", num2, "is", gcd)
112 chars
9 lines

Output:

main.py
The gcd of 48 and 60 is 12
27 chars
2 lines

In this example, we import the math module and assign two numbers, 48 and 60, to the variables num1 and num2, respectively. We then use the math.gcd() function to find their gcd and store the result in the variable gcd. Finally, we print out the gcd using print() function. The output indicates that the gcd of 48 and 60 is 12.

gistlibby LogSnag