UNI_Python/ha_03/loosen_janniclas_1540907_04.py
2023-11-26 18:30:56 +01:00

35 lines
960 B
Python

# Jan-Niclas Loosen
# 1540907
def sieve_of_eratosthenes(limit):
primes = [True] * (limit + 1)
primes[0] = primes[1] = False # 0 and 1 are not primes
for num in range(2, int(limit ** 0.5) + 1): # only necessary to the square root of the limit
if primes[num]:
prime = num
for i in range(1,limit):
mult = prime * (i + 1)
if mult >= len(primes): # more loops are unnecessary
break
primes[mult] = False
return primes
def prime_factorization(n):
primes = sieve_of_eratosthenes(n)
factors = []
for i in range(2, n + 1):
if primes[i] and n % i == 0:
while n % i == 0:
factors.append(i)
n = n // i
return factors
# dialog for testing the functions
num = int(input("insert positive integer: "))
if num < 0 :
print("invalid input")
else:
print(prime_factorization(num))