Skip to content

Instantly share code, notes, and snippets.

@KoStard
Created March 23, 2018 16:20
Show Gist options
  • Save KoStard/2e700a6338ec1145be5263163368effa to your computer and use it in GitHub Desktop.
Save KoStard/2e700a6338ec1145be5263163368effa to your computer and use it in GitHub Desktop.
import math
def task81(n):
d = {}
last_prime = 2
if n == 2:
d = {2 : 1}
else:
while n > 1:
if n%last_prime == 0 and d.get(last_prime) == None:
d[last_prime] = 0
while n%last_prime == 0:
d[last_prime]+=1
n = int(n/last_prime)
found_new = False
for i in range(last_prime+1, int(math.sqrt(n)+1)):
if n%i == 0:
found_new = True
last_prime = i #i should be prime, because if it can be divided to 2, then n can't divide to it, because all 2's were removed from n
break
if not found_new:
last_prime = n
res = ""
for i, d_i in enumerate(d):
res += str(d_i)
if d[d_i]!=1:
res+="^{}".format(d[d_i])
if i<len(d)-1:
res+=" * "
print(res)
# task81(199) -> 199
# task81(525) -> 3 * 5^2 * 7
# task81(2) -> 2
# task81(100) -> 2^2 * 5^2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment