[ create a new paste ] login | about

Link: http://codepad.org/cJ2CXP8h    [ raw code | output | fork ]

Python, pasted on Jun 2:
def is_prime(i, list_primes): # test if i is a prime
    if i in list_primes: # check to see if i is already a known prime
        return True # i is a known prime

    for prime in list_primes: # check each known prime
        if i % prime == 0: # if the known prime evenly divides i, then: 
            return False # i CANNOT be prime
    return True # if we reach this point, i MUST be prime

###########
n = 25 #######the nth prime we are looking for, make your changes here########
###########

list_primes= [2] # this is our list of known prime numbers, we start it off with 2

i = 2 # a counter to start at 2
        
while len(list_primes) < n: # while we haven't found the nth prime
    i = i + 1 # the next number to check if it is prime
    if is_prime(i, list_primes): # check if is prime
        list_primes.append(i) # if it is prime, add it to the list of known primes

output_message = "The %dth prime is: %d" % (n, list_primes[n-1]) # simple output form
print(output_message) # print our output


Output:
1
The 25th prime is: 97


Create a new paste based on this one


Comments: