5.26
正整数的因子展开式
【题目描述】
编写程序,输出一个给定正整数x(x>1)的质因子展开式。
【源代码程序】
def prime_factors(x):
factors = [] # 存储质因子的列表
divisor = 2 # 初始除数为2
# 将x分解成质因子
while x > 1:
if x % divisor == 0:
factors.append(divisor)
x //= divisor
else:
divisor += 1
# 将质因子列表转换为字符串
factors_str = ''.join(str(factor) for factor in factors)
return factors_str
# 输入整数x的值
x = int(input())
# 输出x的质因子展开式
factors_str = prime_factors(x)
print(f"{x}={factors_str}")
【运行测试】

浙公网安备 33010602011771号