网络编程
位置:首页>> 网络编程>> Python编程>> python3 求约数的实例

python3 求约数的实例

作者:飞奔的帅帅  发布时间:2023-12-29 03:18:59 

标签:python3,约数

如下所示:


#求一个数的最大约数(不算本身)
def getmaxnum(n):
 num = n //2
 while num >1:
   if n % num ==0:
     print(num)
     break
   else:
     num = num - 1
 else:
   print('sushu')
getmaxnum(455)

#求最大公约数
#greatest common divisor;gcd
def greatest_common_divisor(m,n):
 if m % n ==0:
   return n
 while m%n !=0:
   m,n = n,m%n
 return n
gcd = greatest_common_divisor(25,120)
print(gcd)

#求最小公倍数
#greatest common divisor;gcd
def greatest_common_divisor(m,n):
 if m % n ==0:
   return n
 while m%n !=0:
   m,n = n,m%n
 return n
gcd = greatest_common_divisor(25,120)
print(gcd)
#两数之积 = 最小公倍数 * 最大公约数
#greatest common multiple 缩写为 gcm
def greatest_common_multiple(m,n):
 gcd=greatest_common_divisor(m,n)
 gcm = (m*n)//gcd
 return gcm
gcm = greatest_common_multiple(18,27)
print(gcm)

来源:https://blog.csdn.net/ustbbsy/article/details/79622103

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com