From f64a9186b53901a5ae9412f0747dbab80ad5de49 Mon Sep 17 00:00:00 2001 From: Loosen-IT Date: Sun, 26 Nov 2023 18:30:56 +0100 Subject: [PATCH] 26/11/2023 --- .idea/misc.xml | 2 +- .idea/python.iml | 2 +- ha_03/loosen_janniclas_1540907_04.py | 34 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 ha_03/loosen_janniclas_1540907_04.py diff --git a/.idea/misc.xml b/.idea/misc.xml index ce0c400..46ad6d9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/python.iml b/.idea/python.iml index b567092..ca820fc 100644 --- a/.idea/python.iml +++ b/.idea/python.iml @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/ha_03/loosen_janniclas_1540907_04.py b/ha_03/loosen_janniclas_1540907_04.py new file mode 100644 index 0000000..9db67f6 --- /dev/null +++ b/ha_03/loosen_janniclas_1540907_04.py @@ -0,0 +1,34 @@ +# 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))