[Solved] Find sum of first 1000 prime numbers in python [duplicate]


Just a small improvement based on your code to find limit primes instead of limit numbers.

limit = 1000

def is_prime(n):
    for i in range(2, n):
        if n%i == 0:
            return False
    return True

sum = 0
num = 2
for i in xrange(limit):
    while not is_prime(num):
        num += 1
    sum += num
    num += 1 # sorry, miss this
print sum

And you could also use a single loop when looking for certain amount of things you are interested in, it could be just a matter of taste.

limit = 1000

def is_prime(n):
    for i in range(2, n):
        if n%i == 0:
            return False
    return True

sum = 0
count = 0
num = 2
while count != limit:
    if is_prime(num):
        sum += num
        count += 1
    num += 1

print sum

3

solved Find sum of first 1000 prime numbers in python [duplicate]