added loops
This commit is contained in:
32
loops/ex01/solution/primes.py
Normal file
32
loops/ex01/solution/primes.py
Normal file
@ -0,0 +1,32 @@
|
||||
def is_prime(n: int) -> bool:
|
||||
if n < 2:
|
||||
return False
|
||||
|
||||
for i in range(2, n):
|
||||
if n % i == 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def next_prime(n: int) -> int:
|
||||
return n + 1 if is_prime(n + 1) else next_prime(n + 1)
|
||||
|
||||
|
||||
def next_prime_iterative(n: int) -> int:
|
||||
num = n + 1
|
||||
while not is_prime(num):
|
||||
num += 1
|
||||
return num
|
||||
|
||||
|
||||
def prime_factorize(n: int) -> list[int]:
|
||||
prime_factores: list[int] = []
|
||||
num = n
|
||||
prime = 0
|
||||
while (prime := next_prime(prime)) <= n:
|
||||
if num % prime == 0:
|
||||
prime_factores += [prime]
|
||||
num //= prime
|
||||
|
||||
return prime_factores
|
Reference in New Issue
Block a user