[Tutor] prime factorisation

Bernd Hewener bhewener at gmail.com
Sun Mar 11 11:23:02 EDT 2018

```Dear python tutors,

for training purposes, I am writing a Python program for prime factorisation.

Functions for reading in the number and finding the primes are working alright (of Course, finding primes up to ½ Number would suffice).

Still, the function for finding the factorization itsel does not work. I am not sure, but the number i running through the primes for checking does not seem to work properly.

Would you please be so kind as to help?

Thank you very much.

Best wishes,

Bernd

# This program calculates the prime factors of a number entered.

primefactors = []

n, num = 0, 0

while True:
num = input("Please enter a positive integer n for prime factorization. ")
try:
n = int(num)
return n
except ValueError or num < 1:
else:
break

def prime_find(l):
#print ("Primefind " + str(l))
primes = []
for num in range(2, l + 1):
for i in primes:
if (num % i) == 0:
break
else:
primes.insert(0, num)
return primes

def prime_factorization(k):
print ("Prime factorization")
prime_facs = []
for i in range(len(prime_find(k)):
while k % i == 0:
prime_facs.insert(0, i)
k = k / i
else:
break
return prime_facs

primes = prime_find(number)
print ("Prime numbers up to " + str(number) + " are:")
print (primes)
prime_factor = prime_factorization(number)
print (prime_factor)

"""def PrimFaktorZerlegung(n):
#Primen enthält Primzahlen. Wir hoffen, dass
#alle Primteiler in diese List zu finden sind.
#Sie kann ein Parameter, oder ein Klasskonstant sein
for p in range(len(Primen)):
while n % p == 0:
Teiler.append(p)
n = n/p
return Teiler"""

```