Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime numbers
#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


Messages In This Thread
Prime numbers - by janoshz - Sep-30-2019, 12:32 PM
RE: Prime numbers - by ichabod801 - Sep-30-2019, 01:08 PM
RE: Prime numbers - by janoshz - Sep-30-2019, 01:34 PM
RE: Prime numbers - by jefsummers - Sep-30-2019, 02:18 PM
RE: Prime numbers - by Preetimishra90 - Oct-05-2019, 10:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 2,329 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 4,185 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Prime numbers calculations frequency 3 3,903 Nov-27-2018, 07:07 PM
Last Post: micseydel
  10x10 Array of Prime Numbers smfox2 1 3,137 Sep-03-2018, 12:36 AM
Last Post: ichabod801
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 5,053 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  PRIME NUMBERS iamnotabot 6 5,836 Sep-17-2017, 04:25 PM
Last Post: iamnotabot
  prime factorization of two large numbers pyJim 2 4,554 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