题解 | #质数因子#
质数因子
https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
import sys import math def prime_gen(upper_bound): # 质数生成器函数 remaining = set(range(2, upper_bound + 1)) # 生成集合存放待测试的数 while remaining: i = min(remaining) # 取最小的数 remaining.discard(i) yield i not_prime = {num for num in remaining if num % i == 0 } remaining.difference_update(not_prime) # 从集合移除被 i 整除的数 number = int(sys.stdin.read()) result = list() quotient = number for prime_to_test in prime_gen(int(math.sqrt(number))): if prime_to_test * prime_to_test > quotient: break while quotient % prime_to_test == 0: quotient /= prime_to_test result.append(prime_to_test) if quotient != 1: result.append(int(quotient)) output_str = ' '.join([str(p) for p in result]) print(output_str)