python質數因式分解

# 質數因式分解
import math

x = int(input("請輸入一個大於10的整數:"))
primes = [p for p in range(2, x // 2 + 1) if 0 not in [p % d for d in range(2, int(math.sqrt(p)) + 1)]]
factorList = []
y = x
for i in primes:
    while y % i == 0:
        factorList.append(i)
        y = y // i
if not factorList:
    print("%d=%d*%d" % (x, 1, x))
else:
    print(factorList)
    s = "*".join(map(lambda item: str(item), factorList))
    print("%d=%s" % (x, s))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章