Re: [Tutor] prime factorisation
I think You would do much better if You wrote pseudo code first, i.e. write each step out in words, code is much easier to write following pseudo code Are You trying to factor Prime Numbers? Prime Number factored (Prime Number and 1) https://en.wikipedia.org/wiki/Table_of_prime_factors#1_to_100 https://www.mathsisfun.com/prime-factorization.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] prime factorisation
Hello Bernd, My comments below, interleaved with yours. On Sun, Mar 11, 2018 at 04:23:02PM +0100, Bernd Hewener wrote: > Functions for reading in the number and finding the primes are working > alright (of Course, finding primes up to ½ Number would suffice). Actually you only need to go up to square root of the number. > 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. What do you mean, does not work? In what way? When you run the code, what does it do? Some further comments: [...] > def readin(): > print ("Readin") > while True: > num = input("Please enter a positive integer n for prime > factorization. ") > try: > n = int(num) > return n > except ValueError or num < 1: > print("Please enter a _positive_ integer!") > else: > break That code doesn't actually do what you think it does. In fact, it is an accident that it works at all! The problem is the line except ValueError or num < 1 which does *not* catch a ValueError and check that num is less than one. Instead, it evaluates to just catching ValueError. So it never checks that the entered number is positive. Regards, Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] prime factorisation
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 def readin(): print ("Readin") while True: num = input("Please enter a positive integer n for prime factorization. ") try: n = int(num) return n except ValueError or num < 1: print("Please enter a _positive_ integer!") 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 number = readin() 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""" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor