#13087: python 供參


weiye (weiye)

學校 : 國立文華高級中學
編號 : 16127
來源 : [61.216.106.3]
最後登入時間 :
2023-03-07 11:02:39
a010. 因數分解 | From: [1.200.36.200] | 發表日期 : 2017-12-03 09:20

from sys import stdin
def factoring(num):
    prime_factors = []
    n=2
    while n<=num:
        if num%n !=0:
            n+=1
        else:
            prime_factors.append(n)
            num/=n
    fact_dict={i:prime_factors.count(i) for i in prime_factors}

    factor_string =""

    for key in sorted(fact_dict):
        if fact_dict[key] == 1:
            factor_string+=str(key)+" * "
        else:
            factor_string+=str(key)+"^"+str(fact_dict[key])+" * "
    
    return factor_string[0:len(factor_string)-2]
    
for line in stdin:
    print(factoring(int(line)))
 
ZeroJudge Forum