将一个正整数分解质因数。例如:输入90,打印出90=2 * 3 * 3 * 5python
时间: 2023-05-16 09:07:33 浏览: 98
以下是将一个正整数分解质因数的 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
num = int(input("请输入一个正整数:"))
factors = prime_factors(num)
print(f"{num}=", end="")
for i in range(len(factors)):
if i == len(factors) - 1:
print(factors[i])
else:
print(factors[i], "*", end="")
```
例如,输入90,输出结果为:
```
90=2*3*3*5
```
相关问题
Python将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*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} = {'*'.join(map(str, factors))}")
```
输出结果为:
```
90 = 2*3*3*5
```
python将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*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
```
阅读全文