zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python质数因式分解

Python 质数
2023-09-14 08:57:24 时间
# 质数因式分解
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))