a010.
因數分解
| From: [1.165.132.248] |
發表日期
:
2022-09-23 23:21
```python
def prime_factors(n: int) -> dict:
factors = {} #存放因數與之出現次數
i = 2
while n>= i: #當n<i(無法繼續除)時退出loop
while n % i == 0:
n //= i
if not factors.get(i, False):
factors[i] = 1
else:
factors[i] += 1
i += 1
return factors
def output_factor(n): #輸出格式化
factors = prime_factors(n)
factors_arr = [f'{factor}{f"^{time}" if time>1 else ""}' for factor, time in factors.items()] #當次方數為1時不輸出次方數
return ' * '.join(factors_arr) 在中間插入乘號
```
不過我不太理解兩個共用相同變數的時間複雜度要怎麼計算