题目描述
Pile 想要知道一个经典问题:
给你两个数,请求出一个数的最大公约数
解答:
a=int(input())
b=int(input())
min=a
if(a>b):
min=b
while min>0:
if(a%min==0 and b%min==0):
break;
min=min-1
print(min)
若使用循环,则会时间超限,原因是系统给的样例很大(如100000)
可以调用math库里的gcd函数来编程解决
import math#调用math库
a=int(input())
b=int(input())
print(math.gcd(a,b))#求最大公约数
关于gcd函数:
math.gcd() method is a library method of math module, it is used to find GCD (Greatest Common Divisor) of given numbers, it accepts two integer numbers and returns their greatest common divisor (the largest positive integer that divides both of the numbers).
math.gcd()方法是数学模块的库方法,用于查找给定数字的GCD(最大公约数),它接受两个整数并返回其最大公约数(将两个整数相除的最大正整数)。