Created
August 25, 2021 13:40
-
-
Save BelegCuthalion/9448bd54ea75014672071901e91e033a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt | |
def prime_factors(n): | |
s = "" | |
l = sorted(factorsList(n)) | |
l.append(-1) # this addintional element helps the last element in l to appear in s | |
# but itself won't appear in s | |
d = l[0] | |
i = 0 | |
for n in l: | |
if n == d: | |
i += 1 | |
else: | |
if i == 1: | |
s += "(" + str(d) + ")" | |
else: | |
s += "(" + str(d) + "**" + str(i) + ")" | |
i = 1 | |
d = n | |
return s | |
def factorsList(n): | |
if n == 1: | |
return [] | |
for i in range(2, int(sqrt(n))+1): | |
if n % i == 0: | |
return [i] + factorsList(n//i) | |
return [n] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment