0
Prime factorisation using degrees. Print problem.
Hi, guys! I have got a problem. How can I print my dictionary this way: 1008= 2^4*3^2*7 This is my code: def factorize(n): ans={} d=2 while d*d<=n: while n%d==0: ans[d]=ans.get(d,0)+1 n//=d d+=1 if n>1: ans[n]=1 ans=dict(ans) return ans n=int(input()) print(factorize(n))
2 Antworten
+ 1
def factorize(n,s,f):
ans={}
d=2
while d*d<=n:
while n%d==0:
ans[d]=ans.get(d,0)+1
n//=d
d+=1
if n>1:
ans[n]=1
ans=dict(ans)
s=ans.keys()
f=ans.values()
s=list(s)
f=list(f)
for i in range(len(s)):
if f[i]!=1 and i!=len(s)-1:
print(s[i],"^",f[i],sep='',end='*')
elif f[i]==1 and i!=len(s)-1:
print(s[i],end="*")
elif f[i]==1 and i==len(s)-1:
print(s[i])
elif f[i]!=1 and i==len(s)-1:
print(s[i],"^",f[i],sep='')
n=int(input())
s=[]
f=[]
factorize(n,s,f)
+ 1
This is how I solved my problem