Prime numbers - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Prime numbers (/thread-21440.html) |
Prime numbers - janoshz - Sep-30-2019 Hello, I have a slight problem with my prime number checking machine :D It does not print out the number 2 it should and does falsely recognize 9 as a prime. This is a homework for a course and the assignment goes beyond this, but I cannot continue before I have solved this problem since we are using a virtual platform. I'm from Finland so the text is in Finnish, but I tried to include comments for clarification. Problems: - does not print out number 2 - falsely recognizes 9 as a prime Thanks for any help you can give! low = int(input("Anna alueen alaraja: ")) # range low end high = int(input("Anna alueen yläraja: ")) # range high end for num in range(low,high+1): if (num == 0) or (num == 1): print(num,"ei ole kelvollinen alkuluku.") # 0 and 1 are not valid primes elif (num != 0) or (num != 1): for div in range(2,num): # testing whether a prime or not if ((num % div) == 0): print(num,"ei ole alkuluku, koska",div,"*",num//div,"=",num) # number is not a prime because ... break else: print(num,"on alkuluku.") # number is prime number break area = len(range(low,high+1)) # How many numbers in range print("Tutkittiin",area,"lukua") # How many numbers chekced by program print("Kiitos ohjelman käytöstä.") # Thanks RE: Prime numbers - ichabod801 - Sep-30-2019 The problem with 2 is that your for loop on line 11 is then for div in range(2, 2): . That range is empty. So the loop never executes and 2 is never checked.The problem with 9 (and every other odd composite number) is that you break out of the for loop as soon as you get a number that is not a divisor. So the first thing you check is 2, which doesn't divide 9 evenly, so you said 9 is prime without going on to check 3, 4, 5, and so on. For loops have a handy else construction. If a for loop has an else, the else triggers if the loop finished without a break statement. So if you just dedent the else on line 16, so that it is associated with the for loop on line 11 instead of the if statement on line 12, it should work: for div in range(2,num): # testing whether a prime or not if ((num % div) == 0): print(num,"ei ole alkuluku, koska",div,"*",num//div,"=",num) # number is not a prime because ... break else: print(num,"on alkuluku.") # number is prime number breakMaybe that is what you meant to do? If you have not covered else for loops, and cannot use it in this homework, I would recommend setting up a flag variable to indicate a number is not prime: is_prime = True for div in range(2,num): # testing whether a prime or not if ((num % div) == 0): print(num,"ei ole alkuluku, koska",div,"*",num//div,"=",num) # number is not a prime because ... is_prime = False break if is_prime: print(num,"on alkuluku.") # number is prime number breakThere are some inefficiencies here, but that's not a big deal for a homework assignment (you don't need to check every number less than n, just every prime less than or equal to sqrt(n)). Note that the elif on line 9 can just be an else. Every number is either not 0 or not 1, since no number can be both 0 and 1. So that clause is always True, which is what you want any way. RE: Prime numbers - janoshz - Sep-30-2019 Yeah, I got it now. I needed to move the else and then added continue to it and got it working as the platform wanted. Thank you very much! :) RE: Prime numbers - jefsummers - Sep-30-2019 One other issue - in line 9 you logically "or" that the number is not 0 and not 1. 0 is not 1, and 1 is not 0, so that will always evaluate as true. Changing that line to simply elseshould fix it. RE: Prime numbers - Preetimishra90 - Oct-05-2019 A prime number is a whole number greater than 1 whose only factors are 1 and itself. The list of first some prime numbers is 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. |