Skip to content

Instantly share code, notes, and snippets.

@Ken-Kuroki
Created October 6, 2019 13:20
Show Gist options
  • Save Ken-Kuroki/8b392a34fdc11fbc531b3310d72a5f58 to your computer and use it in GitHub Desktop.
Save Ken-Kuroki/8b392a34fdc11fbc531b3310d72a5f58 to your computer and use it in GitHub Desktop.
Find prime factors
def prime_factorize(n):
answers = []
while n % 2 == 0:
answers.append(2)
n //= 2
f = 3
while f**2 <= n: # instead of f <= n**0.5
if n % f == 0:
answers.append(f)
n //= f
else:
f += 2
if n != 1:
answers.append(n)
return answers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment