[python practice questions] program 14

#Question : Decompose a positive integer into prime factors. For example: input 90, print out 90=2*3*3*5. 
#My method should be more concise than online, but the recursion may be slower 
n = input( ' Please enter a positive integer: ' )

n = int(n)

X = str(n)

m = []
 def zhengchu(n):

    for i in range(2,n+1):
        if n % i == 0:
            k = int(n / i)
            m.append(i)
            return zhengchu(k)
        else:
            continue
    y = [str(x) for x in m]
    print ('%s = '% X,'*'.join(y))


zhengchu(n)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325060334&siteId=291194637