User:Milanandreew/Python 3 Programming/Conditional Statements/Solution

From Wikibooks, open books for an open world
Jump to navigation Jump to search
# initialise state variables
lastPrime = 2
currentAttempt = 3
primesLeft = 999 # We already know 2 is prime
divideBy = 2
while (primesLeft>0):
    isPrime = True
    # square root is upper bound,
    # because if the divisor is smaller than the square root, the quotient is larger
    while (divideBy <= currentAttempt**(1/2)):
        if currentAttempt%divideBy == 0:
            isPrime = False
        divideBy += 1
    if isPrime == True:
        # print(currentAttempt, 'is prime')
        primesLeft = primesLeft - 1
        lastPrime = currentAttempt
    # reset state variables
    currentAttempt = currentAttempt + 1
    divideBy = 2
print(lastPrime, 'is the 1000th prime number')