题解 | 质数因子

质数因子

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

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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