To find first N prime numbers in python

前端 未结 29 2325
醉梦人生
醉梦人生 2020-11-28 06:56

I am new to the programming world. I was just writing this code in python to generate N prime numbers. User should input the value for N which is the total number of prime n

29条回答
  •  感动是毒
    2020-11-28 07:48

    This code is very confused, and I can't figure out exactly what you were thinking when you wrote it or what you were attempting to accomplish. The first thing I would suggest when trying to figure out how to code is to start by making your variable names extremely descriptive. This will help you get the ideas of what you're doing straight in your head, and it will also help anyone who's trying to help you show you how to get your ideas straight.

    That being said, here is a sample program that accomplishes something close to the goal:

    primewanted = int(input("This program will give you the nth prime.\nPlease enter n:"))
    if primewanted <= 0:
        print "n must be >= 1"
    else:
        lastprime = 2 # 2 is the very first prime number
        primesfound = 1  # Since 2 is the very first prime, we've found 1 prime
        possibleprime = lastprime + 1 # Start search for new primes right after
        while primesfound < primewanted:
            # Start at 2.  Things divisible by 1 might still be prime
            testdivisor = 2
            # Something is still possibly prime if it divided with a remainder.
            still_possibly_prime = ((possibleprime % testdivisor) != 0)
            # (testdivisor + 1) because we never want to divide a number by itself.
            while still_possibly_prime and ((testdivisor + 1) < possibleprime):
                testdivisor = testdivisor + 1
                still_possibly_prime = ((possibleprime % testdivisor) != 0)
            # If after all that looping the prime is still possibly prime,
            # then it is prime.
            if still_possibly_prime:
                lastprime = possibleprime
                primesfound = primesfound + 1
            # Go on ahead to see if the next number is prime
            possibleprime = possibleprime + 1
        print "This nth prime is:", lastprime
    

    This bit of code:

            testdivisor = 2
            # Something is still possibly prime if it divided with a remainder.
            still_possibly_prime = ((possibleprime % testdivisor) != 0)
            # (testdivisor + 1) because we never want to divide a number by itself.
            while still_possibly_prime and ((testdivisor + 1) < possibleprime):
                testdivisor = testdivisor + 1
                still_possibly_prime = ((possibleprime % testdivisor) != 0)
    

    could possibly be replaced by the somewhat slow, but possibly more understandable:

            # Assume the number is prime until we prove otherwise
            still_possibly_prime = True
            # Start at 2.  Things divisible by 1 might still be prime
            for testdivisor in xrange(2, possibleprime, 1):
                # Something is still possibly prime if it divided with a
                # remainder.  And if it is ever found to be not prime, it's not
                # prime, so never check again.
                if still_possibly_prime:
                    still_possibly_prime = ((possibleprime % testdivisor) != 0)
    

提交回复
热议问题