Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created June 1, 2017 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanplopes/e5d57cbd171f9e1cc0d6890b3d7102b2 to your computer and use it in GitHub Desktop.
Save juanplopes/e5d57cbd171f9e1cc0d6890b3d7102b2 to your computer and use it in GitHub Desktop.
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