python 最大公约数 最小公倍数

def gongyueshu(m,n):
if m<n:
m,n=n,m
elif m==n:
return m
if m/n==int(m/n):
return n
else:
for i in range(n,0,-1):
if m/i==int(m/i) and n/i==int(n/i):
return i def gongbeishu(m,n):
aa=[]
if m<n:
m,n=n,m
elif m==n:
return m
  while gongyueshu(m,n)!=1:
for i in range(2,int(n)+1):
if m/i==int(m/i) and n/i==int(n/i):
m=int(m/i)
n=int(n/i)
aa.append(i)
break
cc=1
for b in aa:
cc=b*cc
cc=cc*n*m
return cc print(gongyueshu(18,3))
print(gongbeishu(18,3))

结果:

================== RESTART: D:\Python\Python37\gongyueshu1.py ==================
3
18
>>>

def lcm(x, y):
"""该函数返回两个数的最小公倍数"""
# 获取最大的数
if x > y:
greater = x
else:
greater = y while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1 return lcm
def hcf(x, y):
"""该函数返回两个数的最大公约数""" # 获取最小值
if x > y:
smaller = y
else:
smaller = x for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i return hcf
上一篇:go语言通道详解


下一篇:xml(3)--dom4j实现crud操作