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))

 

posted @ 2021-05-28 11:15  前方、有光  阅读(383)  评论(0编辑  收藏  举报