Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Given a positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form :
- "(p1**n1)(p2**n2)...(pk**nk)"
- with the p(i) in increasing order and n(i) empty if n(i) is 1.
- @EXAMPLE:
- n = 86240 should return "(2**5)(5)(7**2)(11)"
- n= 782611830 should return "(2)(3**2)(5)(7**2)(11)(13)(17)(73)"
- -------------------------------------------------------------------------------------------------------------------------------------
- import math
- def prime_factors(n):
- result=''
- count = 0
- while n%2==0:
- n/=2
- count+=1
- if count==1:
- x2=("("+"2"+")")
- for cnt01 in x2:
- result+=cnt01
- elif count:
- x= ("(" + "2**" + str(count) + ")" )
- for cnt02 in x:
- result+=cnt02
- for k in range(3,int(math.sqrt(n)+1),2):
- count=0
- while n%k==0 and n!=1:
- n/=k
- count+=1
- if count==1:
- z=("("+str(k)+")")
- for cnt in z:
- result+=cnt
- elif count:
- y= ("("+str(k) + "**" + str(count)+")")
- for cnt1 in y:
- result+=cnt1
- if n > 2:
- x1=("("+str(int(n))+")")
- for cnt2 in x1:
- result+=cnt2
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement