题解 | 质数因子

质数因子

https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607

n = int(input())
factors = []

# 处理2的因子
while n % 2 == 0:
    factors.append(2)
    n = n // 2

# 处理奇数因子
i = 3
max_factor = int(n**0.5) + 1
while i <= max_factor:
    while n % i == 0:
        factors.append(i)
        n = n // i
        max_factor = int(n**0.5) + 1
    i += 2

# 如果剩下的n是大于1的质数
if n > 1:
    factors.append(n)

# 输出结果
print(' '.join(map(str, factors)))

全部评论

相关推荐

2025-12-22 16:31
已编辑
桂林电子科技大学 Python
很奥的前端仔:如果你接了offer 临时又说不去 hr确实要多做一些工作。 当然如果是接offer之前当我没说
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务