简体   繁体   中英

Python prime number code

I am trying to generate the first "n" prime numbers after 3. Issue is I do not understand where my code is going wrong and would appreciate if someone could help me with the logic.

a = 3
b = 2

# n = number of prime numbers to be printed.
n = 1000

for a in range(a, n):   #This i the range of all numbers to be tested
    if a % 2 == 0:      #Only odd numbers are prime(except 2) so eliminate the evens
        print "."
else:
    for b in range(b, a):  #This is to test the odd numbers
        if (a % b == 0):   #I am dividing  a by all numbers smaller than it to test
            print a, "is not prime"
            break

Any guidance on helping me solve this problem would be greatly appreciated. I read through the older posts on this question but wanted to see where specifically my own logic was flawed.

Thanks.

I think you have several issues here.

Your indentation is wrong when it comes to the else.

Also, try to change the variables name you use in your loops to not use the a and b defined at the top of your script and you should see a difference.

It should be :

a = 3
b = 2

n = 1000

for ax in range(a, n):   
   if ax % 2 == 0:
       print "."
   else:
       for bx in range(b, ax): 
            if (ax % bx == 0):   
                print ax, "is not prime"
                break

Adding another else after the break should also help you in debugging this kind of issues.

First you should check the indentation. I guess this is your code:

for a in range(a, n):   #This i the range of all numbers to be tested
    if a % 2 == 0:      #Only odd numbers are prime(except 2) so eliminate the evens
        print "."
    else:
        for b in range(b, a):  #This is to test the odd numbers
            if (a % b == 0):   #I am dividing  a by all numbers smaller than it to test
                print a, "is not prime"
                break

This code makes sense to me. You need to fix the first print too.

    if a % 2 == 0:      #Only odd numbers are prime(except 2) so eliminate the evens
        print a, "is not prime"

Now lets see the logic. If a is even, then it is not prime, you can't start with a=2. Then, if it is odd, check if any number less than can divide it.

There are better algorithm to do this. Check this .

Here's some code:

import math
import itertools

def isprime(m):
    return not any((m % d == 0 for d in range(2, int(math.sqrt(m)+1))))

def genprimes(n):
    i = 0
    for m in itertools.count(2):  # 2, 3, 4, ... 
        if i >= n:
            return
        if isprime(m):
            yield m
            i += 1

Running

list(genprimes(10))

generates

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    n = 1000
    primes = []
    nonprime = []

    def prime(nonprime, primes, n):
        nonprime.append(0)
        nonprime.append(1)
        nonprime.append(2)
        primes.append(3)
        for number in range(3, n):
            if (number % 2 == 0):
                nonprime.append(number)
            else:
                counter = 3
                while counter <= number:
                    if (counter != number) and (number % counter == 0):
                        nonprime.append(number)
                        break
                    elif (number % counter == 1) and (counter >= number/2):
                        primes.append(number)
                        break
                    else:
                        counter += 1

        print "primes are : ", primes
        print "nonprimes are : ", nonprime

    if __name__ == "__main__":
        prime(nonprime, primes, n)

Obviously not the most efficient. Hopefully it can help somehow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM