Created
June 1, 2017 19:59
-
-
Save juanplopes/e5d57cbd171f9e1cc0d6890b3d7102b2 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
def primes_up_to(N): | |
sieve = N*[True] | |
for i in range(2, N): | |
if not sieve[i]: continue | |
yield i | |
for j in range(i*i, N, i): | |
sieve[j] = False | |
def primeFactors(N): | |
answer = '' | |
for p in primes_up_to(int(N**0.5)+1): | |
count = 0 | |
while N % p == 0: | |
N //= p | |
count += 1 | |
if count == 1: | |
answer += '({})'.format(p) | |
elif count > 1: | |
answer += '({}**{})'.format(p, count) | |
if N>1: | |
answer += '({})'.format(N) | |
return answer | |
print(primeFactors(86240)) | |
print(primeFactors(7775460)) | |
print(primeFactors(7919)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment