Find the greatest common divisor function gcd of two numbers based on python

1. gcd function

In Python , you can use the gcd () function in the math module to find the greatest common divisor of two numbers. First you need to import the math module, and then use the gcd () function to calculate the greatest common divisor.

2. Example

The following is an example code for finding the greatest common divisor of two numbers using Python :

import math
# 定义两个数
a = 36
b = 48
# 求最大公约数
gcd_result = math.gcd(a, b)
# 输出结果
print("最大公约数为:", gcd_result)

In this example, we imported the math module and then defined two numbers a and b . Next, use the math.gcd () function to calculate the greatest common divisor of a and b and save the result in the gcd_result variable. Finally, use the print() function to output the results to the console.

You can replace a and b in the code with the two numbers you need to find the greatest common divisor, and then run the code to get the result of the greatest common divisor.

If you do not have a python running environment, you can use an online python editor. Online python editor - https://c.runoob.com/compile/9/

Enter the program on the left, click Run, and output the results on the right.

Guess you like

Origin blog.csdn.net/weixin_45770896/article/details/132917557