30 lines
545 B
Python
30 lines
545 B
Python
from typing import Iterator
|
|
|
|
|
|
def is_prime(n: int) -> bool:
|
|
if n < 2:
|
|
return False
|
|
for d in range(2, n // 2 + 1):
|
|
if n % d == 0:
|
|
return False
|
|
return True
|
|
|
|
|
|
def primes() -> Iterator[int]:
|
|
num = 2
|
|
while True:
|
|
if is_prime(num):
|
|
yield num
|
|
num += 1
|
|
|
|
|
|
def prime_factorize(n: int) -> Iterator[int]:
|
|
it = primes()
|
|
num = n
|
|
while num > 1:
|
|
if num % (prime := next(it)) != 0:
|
|
continue
|
|
num //= prime
|
|
it = primes()
|
|
yield prime
|