Skip to content

Instantly share code, notes, and snippets.

@ra0x3
Last active November 9, 2019 20:53
Show Gist options
  • Save ra0x3/e9a96960f9fdc8daf2a6919e1607bd87 to your computer and use it in GitHub Desktop.
Save ra0x3/e9a96960f9fdc8daf2a6919e1607bd87 to your computer and use it in GitHub Desktop.
Prime number factorization function
import os
import sys
from typing import *
def is_prime(n: int) -> bool:
if n < 2:
return False
return all(n % i for i in range(2, int(math.sqrt(n)) + 1))
def get_prime_factors(n: int) -> List[int]:
prime_factors: List[int] = []
d = 2
while d * d <= n:
while n % d == 0:
n //= d
prime_factors.append(d)
d += 1
if n > 1: # to avoid 1 as a factor
assert d <= n
prime_factors.append(n)
return prime_factors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment