用Python将一个正整数分解质因数。例如:输入90,打印出90=233*5
时间: 2024-05-04 19:16:09 浏览: 121
好的,您可以使用以下Python代码实现将正整数分解质因数:
```python
def prime_factors(n):
factors = []
d = 2
while n > 1:
while n % d == 0:
factors.append(d)
n //= d
d += 1
if d * d > n:
if n > 1:
factors.append(n)
break
return factors
def format_factors(n):
factors = prime_factors(n)
return "*".join(str(f) for f in factors)
number = 90
factors = format_factors(number)
print(f"{number}={factors}")
```
输出结果为:
```
90=2*3*3*5
```
相关问题
Python编程:一个正整数分解质因数。例如:输入90,打印出90=233*5。
好的,这是一个编程问题,我可以回答。以下是 Python 代码:
```python
def factorize(n):
factors = []
d = 2
while n > 1:
while n % d == :
factors.append(d)
n //= d
d += 1
if d * d > n:
if n > 1:
factors.append(n)
break
return factors
n = int(input("请输入一个正整数:"))
factors = factorize(n)
print(f"{n}=", end="")
for i, factor in enumerate(factors):
if i > :
print("*", end="")
print(factor, 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
```
阅读全文