Created
October 6, 2019 13:20
-
-
Save Ken-Kuroki/8b392a34fdc11fbc531b3310d72a5f58 to your computer and use it in GitHub Desktop.
Find prime factors
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 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