最大公约数,最小公倍数,质因式分解
生活随笔
收集整理的這篇文章主要介紹了
最大公约数,最小公倍数,质因式分解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
簡單代碼算出最大公約數(shù),最小公倍數(shù):
輾轉相除法得到最大公約數(shù):
兩數(shù)相乘倒序最小公倍數(shù):
分解質因式, 打印輸出, 并且存到列表
主程序:
?由質因式得到最小公倍數(shù):
簡單代碼算出最大公約數(shù),最小公倍數(shù):
# 最大公約數(shù)和最小公倍數(shù) a = int(input('please enter 1st num:')) b = int(input('please enter 2nd num:')) s = a * b while a % b != 0:a, b = b, (a % b) print(b, '是最大公約數(shù)') print(s//b, '是最小公倍數(shù)')輾轉相除法得到最大公約數(shù):
# 思想:求出每個正數(shù)的質因式子,max和for循環(huán)實現(xiàn)
def max_div(m, n):# 得到最大公約數(shù)#算法:除法, 函數(shù)作用較為單一r = 1while True:r = m % nif r == 0:return nm = nn = r兩數(shù)相乘倒序最小公倍數(shù):
def min_times(m, n):# 這里不要用 m = max(m, n) n = min(m, n)后面的n比較的值是更新的m和原來的n_min = m * nfor i in range(m * n, m - 1, -1):if i % m == 0 and i % n == 0 and i < _min:_min = ireturn _min分解質因式, 打印輸出, 并且存到列表
def fun(n):x = 1flag = Truelst = []print("{}=".format(n), end='')while n != 1:x += 1while n % x == 0:lst.append(x)n /= xif flag:flag = Falseprint(x, end='')else:print("*{}".format(x), end='')print()return lst主程序:
if __name__ == '__main__':m = int(input('Enter a integer number:'))n = int(input('Enter the second integer number:'))print('最大的公約數(shù)為', max_div(m, n))print('最小公倍數(shù)為', min_times(m, n))?由質因式得到最小公倍數(shù):
l1 = fun(m)l2 = fun(n)for i in l1:if i not in l2:l2.append(i)k = 1for i in l2:k *= iprint('最小公倍數(shù)為:', k)總結
以上是生活随笔為你收集整理的最大公约数,最小公倍数,质因式分解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样给无线网加密
- 下一篇: WARNING: Ignoring in