lundi 24 août 2015

Python: find 10th prime number

Here is my code:

n = 0 
counter = []

while len(counter) < 10:
    n += 1 
    for k in range(2, n):
        if n % k == 0: 
            pass 
        elif n % k != 0:
            counter.append(n)
            break  
print counter 

The result is as follows: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Can someone help me with this code or some separate code that does the same purpose? Also, can someone explain to me why 6, 9, and 12 are in this list? I thought the for loop with the if and elif statement would have solved this already.

Aucun commentaire:

Enregistrer un commentaire