Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime numbers
#1
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
Reply
#2
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
            break
Maybe 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
            break
There 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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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! :)
Reply
#4
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
else
should fix it.
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,399 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 2,982 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Prime numbers calculations frequency 3 2,969 Nov-27-2018, 07:07 PM
Last Post: micseydel
  10x10 Array of Prime Numbers smfox2 1 2,523 Sep-03-2018, 12:36 AM
Last Post: ichabod801
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,085 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  PRIME NUMBERS iamnotabot 6 4,587 Sep-17-2017, 04:25 PM
Last Post: iamnotabot
  prime factorization of two large numbers pyJim 2 3,672 Aug-24-2017, 03:19 PM
Last Post: pyJim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020