-5

I have made a program to find primes, but when I try to get the 10001st prime it isn't correct. I can't figure out why.

I haven't really tried much because I have no idea what to try.

import math
startingPrimes = [2,3, 5, 7, 11, 13, 17, 19]
for times in range (2):
    primeList = []
    numbersToBeTested = 0
    for _ in range (startingPrimes[len(startingPrimes)-1]**2):
        primeList.append(_ + 2)
    print ()
    numbersToBeTested = startingPrimes[len(startingPrimes)-1]**2
    print (numbersToBeTested)
    divisor = 0
    term = 0
    dividend = 0
    position = 0

    while divisor < math.sqrt(len(primeList)):
        term = 0
        divisor = startingPrimes[position]
        dividend = primeList[position]
        while term + 1 < len(primeList):

            term = term + 1
            if primeList[term] % divisor == 0:
                if primeList[term] != divisor:
                    primeList.remove(primeList[term])
        position = position + 1
    startingPrimes = primeList

for termOfList in range (len(primeList)):
    print (primeList[termOfList])
print ("How many primes: " + str(len(primeList)))
print ("10001st prime: " + str(primeList[10000]))

It gives me 90373 which is wrong! Please help!

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – ruohola May 27 '19 at 08:11

1 Answers1

1

There are a number of issues with your code:

It generates primes that aren't prime, e.g. 99973 = 257 * 389

It's nearly impossible to figure how it decides when it's found sufficient primes.

It has bizarre expressions that seem to make no sense, eg.:

   for times in range (2):
       primeList = []

This appears to run the code twice! But it changes the result! Where's the code comment!

And there's this:

while divisor < math.sqrt(len(primeList)):

The square root of the number of primes you found so far is a limit when searching for your next prime?

Problematic programs tend to be underwritten or overwritten -- this is an example of an overwritten program. You can solve this problem with a dozen lines of Python, of no more than roughly 30 characters each, in a fraction of a second. Your program requires more than twice that many lines, some twice that length, and takes nearly a minute to come up short on the answer.

My advice is to start over and keep it simple:

You don't need startingPrimes = [2,3, 5, 7, 11, 13, 17, 19]. Just initializing primeList = [2] is sufficient to avoid testing even numbers and focus on odd divisors.

You don't need math.sqrt(), simply test if if divisor * divisor > number: and all the divisors you need are in primeList.

The only modification operation you really need to do on primeList is append().

Somewhere there should be an explicit test for completion, eg.:

while len(primeList) < n:  # where n = 10001
cdlane
  • 40,441
  • 5
  • 32
  • 81