python将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
时间: 2023-12-02 21:03:37 浏览: 197
python 将一个正整数分解质因数 例如:输入90,打印出90=233*5 (示例)
好的,以下是 Python 代码实现:
```python
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
n = 90
factors = prime_factors(n)
print(f"{n}=", end="")
for i in range(len(factors)):
if i == len(factors) - 1:
print(factors[i])
else:
print(factors[i], "*", end="")
```
输出结果为:
```
90=2*3*3*5
```
阅读全文