You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
959 B
34 lines
959 B
# 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)) |