当前位置:主页 > 软件编程 > Python代码 >

python3 求约数的实例

时间:2021-12-19 10:50:01 | 栏目:Python代码 | 点击:

如下所示:

#求一个数的最大约数(不算本身)
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)

您可能感兴趣的文章:

相关文章