Problem:


素数の和

p(i) を小さい方から i 番目の素数とします。例えば、7 は、2, 3, 5, 7 と小さい方から 4 番目の素数なので、p(4) = 7 です。

n が与えられたとき、i = 1 から n までの p(i) の和 s

s = p(1) + p(2) + .... + p(n)

を出力するプログラムを作成してください。例えば、n = 9 のとき、s = 2 + 3 + 5 + 7 + 11 + 13 + 17 + 19 + 23 = 100 となります。

Input

複数のデータセットが与えられます。各データセットに整数 n  (n ≤ 10000) が与えられます。n が 0 のとき入力の最後とします。データセットの数は 50 を超えません。

Output

各データセットの n に対して、s を１行に出力して下さい。

Sample Input

2
9
0

Output for the Sample Input

5
100



Sample input:
2
9
0


Sample output:
5
100

Submission status: Time Limit Exceeded

Solution code:
from math import sqrt, ceil, pow

class SieveOfAtkin:
    """ ??¢???????????????
    https://github.com/mccricardo/sieve_of_atkin/blob/master/sieve_of_atkin.py
    """
    def __init__(self, limit):
        self.limit = limit
        self.primes = []
        self.sieve = [False] * (self.limit + 1)

    def flip(self, prime):
        try:
            self.sieve[prime] = True if self.sieve[prime] == False else False
        except KeyError:
            pass

    def invalidate(self, prime):
        try:
            if self.sieve[prime] == True: self.sieve[prime] = False
        except KeyError:
            pass

    def isPrime(self, prime):
        try:
            return self.sieve[prime]
        except KeyError:
            return False

    def getPrimes(self):
        testingLimit = int(ceil(sqrt(self.limit)))

        for i in range(testingLimit):
            for j in range(testingLimit):
                # n = 4*i^2 + j^2
                n = 4 * int(pow(i, 2)) + int(pow(j, 2))
                if n <= self.limit and (n % 12 == 1 or n % 12 == 5):
                    self.flip(n)

                # n = 3*i^2 + j^2
                n = 3 * int(pow(i, 2)) + int(pow(j, 2))
                if n <= self.limit and n % 12 == 7:
                    self.flip(n)

                # n = 3*i^2 - j^2
                n = 3 * int(pow(i, 2)) - int(pow(j, 2))
                if n <= self.limit and i > j and n % 12 == 11:
                    self.flip(n)

        for i in range(5, testingLimit):
            if self.isPrime(i):
                k = int(pow(i, 2))
                for j in range(k, self.limit, k):
                    self.invalidate(j)

        self.primes = [2, 3] + [x for x in range(len(self.sieve)) if self.isPrime(x) and x >= 5]
        return self.primes



def find_nth_prime(n):
    """ n???????????§????´???°????±??????? """
    A = SieveOfAtkin(110000)
    A.getPrimes()
    primes = A.primes[:n]
    return primes


if __name__ == '__main__':
    # ??????????????\???
    while True:
        data = int(input())
        if data == 0:
            break

        # ????????¨???????????¨???
        result = find_nth_prime(data)
        print(sum(result))

Submission status: Time Limit Exceeded

Solution code:
p = [0,2,3]
while 1:
    n = int(input())
    if n == 0:break
    s = 0
    for i in range(1, n + 1):
        if i < len(p):s += p[i]
        else:
            j = 2
            while 1:
                x = p[-1] + j
                for k in p[1:]:
                    if x % k == 0:break
                else:
                    p.append(x)
                    s += x
                    break
                j += 2
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools


def solv(n):
    r = 0
    i = 0
    for p in primes():
        i += 1
        if i <= n:
            r += p
        else:
            break
    return r


def primes():
    p = []
    p.append(2)
    yield 2
    p.append(3)
    yield 3
    for n in itertools.count(6, 6):
        n1 = n - 1
        for x in p:
            if n1 % x == 0:
                break
        else:
            p.append(n1)
            yield n1
        n2 = n + 1
        for y in p:
            if n2 % y == 0:
                break
        else:
            p.append(n2)
            yield n2


while True:
    n = int(sys.stdin.readline())
    if n == 0:
        exit()
    print solv(n)

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


def solv(n):
    r = 0
    i = 0
    for p in primes():
        i += 1
        if i <= n:
            r += p
        else:
            break
    return r


def primes():
    p = []
    p.append(2)
    yield 2
    p.append(3)
    yield 3
    for n in itertools.count(6, 6):
        p2 = filter(lambda x: x <= n ** 0.5, p)
        n1 = n - 1
        for x in p2:
            if n1 % x == 0:
                break
        else:
            p.append(n1)
            yield n1
        n2 = n + 1
        for y in p2:
            if n2 % y == 0:
                break
        else:
            p.append(n2)
            yield n2


while True:
    n = int(sys.stdin.readline())
    if n == 0:
        exit()
    print solv(n)

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


sum_of_primes = []


def solv(n):
    if n < len(sum_of_primes):
        return sum_of_primes[n-1]
    else:
        i = 0
        for p in prime_list():
            i += 1
            if i <= len(sum_of_primes):
                pass
            else:
                if not sum_of_primes:
                    sop = p
                else:
                    sop = sum_of_primes[-1] + p
                sum_of_primes.append(sop)
                if i == n:
                    return sop


def prime_list():
    primes = []
    primes.append(2)
    yield 2
    primes.append(3)
    yield 3
    for n in itertools.count(6, 6):
        p2 = filter(lambda x: x <= n ** 0.5, primes)
        n1 = n - 1
        for x in p2:
            if n1 % x == 0:
                break
        else:
            primes.append(n1)
            yield n1
        n2 = n + 1
        for y in p2:
            if n2 % y == 0:
                break
        else:
            primes.append(n2)
            yield n2


while True:
    n = int(sys.stdin.readline())
    if n == 0:
        exit()
    print solv(n)

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


sum_of_primes = []


def solv(n):
    if n < len(sum_of_primes):
        return sum_of_primes[n-1]
    else:
        i = 0
        for p in prime_list():
            i += 1
            if i <= len(sum_of_primes):
                pass
            else:
                if not sum_of_primes:
                    sop = p
                else:
                    sop = sum_of_primes[-1] + p
                sum_of_primes.append(sop)
                if i == n:
                    return sop


def prime_list():
    primes = []
    primes.append(2)
    yield 2
    primes.append(3)
    yield 3
    for n in itertools.count(6, 6):
        p2 = filter(lambda x: x <= n ** 0.5, primes)
        n1 = n - 1
        for x in p2:
            if n1 % x == 0:
                break
        else:
            primes.append(n1)
            yield n1
        n2 = n + 1
        for y in p2:
            if n2 % y == 0:
                break
        else:
            primes.append(n2)
            yield n2


ns = []
while True:
    n = int(sys.stdin.readline())
    if n == 0:
        break
    ns.append(n)
nmax = max(ns)
solv(nmax)
for m in ns:
    print sum_of_primes[m-1]

Submission status: Accepted

Solution code:
def sieve(n):
    num = [True] * n
    num[0] = num[1] = False
    for i in range(2,int(n**0.5)+1):
        if num[i]:
            for j in range(i**2, n, i):
                num[j] = False
    return [i for i in range(2, n) if num[i]]

prime = sieve(110000)

while True:
	n = int(input())
	if n == 0: break
	print(sum(prime[:n]))

Submission status: Accepted

Solution code:
isPrime = [True] * 200001
sum = 0
sumPrime = [sum]
def calcPrime():
    global sum
    isPrime[0] = isPrime[1] = False
    for i in range(len(isPrime)):
        if(isPrime[i]):
            sum += i
            sumPrime.append(sum)
            for j in range(2 * i, len(isPrime), i):
                isPrime[j] = False
                
calcPrime()
while(True):
    n = input()
    if(n == 0):
        break
    print(sumPrime[n])

Submission status: Accepted

Solution code:
maxnum = 110000
primes = [True]*maxnum
primes[0] = primes[1] = False
for i in xrange(maxnum):
    if i >= maxnum**0.5 : break
    if not primes[i]: continue
    for j in xrange(i*2,maxnum,i): primes[j] = False
p = [ i for i in xrange(maxnum) if primes[i] ]

while True:
    n = int(raw_input())
    if n == 0: break
    print sum( [ p[i] for i in xrange(n) ] )

Submission status: Accepted

Solution code:
import sys
f = sys.stdin


def create_prime(n):
    prime = [1] * (n + 1)
    prime[:2] = [0, 0]
    for i in range(len(prime)):
        if prime[i]:
            for j in range(2 * i, len(prime), i):
                prime[j] = 0
    return prime

prime = create_prime(200000)

while True:
    n = int(f.readline())
    if n == 0:
        break
    s = 0
    for i in range(len(prime)):
        if prime[i]:
            s += i
            n -= 1
            if n == 0:
                print(s)
                break

Submission status: Accepted

Solution code:
import math
def pri(n):
    if n < 2:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False

    i = 3
    while i <= math.sqrt(n):
        if n % i == 0:
            return False
        i += 2
    return True

l=[i for i in range(110000)if pri(i) ]

while True:
    n = int(input())
    if n == 0: break
    print(sum(l[:n]))

Submission status: Accepted

Solution code:
import math
def pri(n):
    l =[True] * n
    l[0] = l[1] = False
    for i in range(2,n):
        if l[i]:
            for j in range(i*2, n, i):
                l[j] = False
    return l
n=105000
s=pri(n)
l=[i for i in range(n) if s[i]]
while True:
    n = int(input())
    if n == 0: break
    print(sum(l[:n]))

Submission status: Accepted

Solution code:
def prime_table(n):
  list = [True for _ in xrange(n + 1)]
  i = 2
  while i * i <= n:
    if list[i]:
      j = i + i
      while j <= n:
        list[j] = False
        j += i
    i += 1

  table = [i for i in xrange(n + 1) if list[i] and i >= 2]
  return table

table = prime_table(105000)
while True:
    n = int(raw_input())
    if n == 0:
        break
    print sum(table[:n])

Submission status: Accepted

Solution code:
import math
p = [2]
NM = 10000
c = 3
while len(p) < NM:
    m = int(math.sqrt(c))
    r = True
    for i in p:
        if i > m:
            break
        if c % i == 0:
            r = False
            break
    if r:
        p.append(c)
    c += 2
 
while True:
    n = int(raw_input())
    if n == 0:
        break
    print sum(p[:n])

Submission status: Accepted

Solution code:
def prime(t):
    i=2
    while i**2<=t:
        if t%i==0:
            return 0
        i+=1
    return 1
l=[]
for i in range(2,104740):
    if prime(i):
        l.append(i)
while 1:
    n=int(raw_input())
    if n==0:
        break
    ans=0
    while n>0:
        n-=1
        ans+=l[n]
    print ans

Submission status: Accepted

Solution code:
def sieve(n):
    num = [True] * n
    num[0] = num[1] = False
    for i in range(2,int(n**0.5)+1):
        if num[i]:
            for j in range(i**2, n, i):
                num[j] = False
    return [i for i in range(2, n) if num[i]]
 
prime = sieve(110000)
 
while True:
    n = int(input())
    if n == 0: break
    print(sum(prime[:n]))

Submission status: Accepted

Solution code:
def sieve(n):
	p=[True]*n
	p[0]=p[1]=False
	for i in range(2,int(n**0.5)+1):
		if p[i]:
			for j in range(i*i,n,i):
				p[j]=False
	prime =[i for i in range(2,n) if p[i]]
	return prime


def function(n):
	return sum(A[:n])

A=sieve(110000)
while True:
	n=int(input())
	if n==0:
		break
	print(function(n))

Submission status: Accepted

Solution code:
import math
p, c = [2], 3
while len(p) < 10000:
    m = int(math.sqrt(c))
    r = True
    for i in p:
        if i > m:
            break
        if c % i == 0:
            r = False
            break
    if r:
        p.append(c)
    c += 2
  
while True:
    n = int(raw_input())
    if n == 0:
        break
    print sum(p[:n])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

primes = [2]
i = 3
count = 1
while count < 10000:
    flag = True
    for p in primes:
        if i % p == 0:
            flag = False
            break
    if flag:
        primes.append(i)
        count += 1
    i += 1

while True:
    n = int(raw_input())
    if n == 0:
        break
    sum = 0
    for i in range(n):
        sum += primes[i]
    print sum

Submission status: Accepted

Solution code:
def make_ps(n):
    nums = [True] * n
    nums[0] = nums[1] = False
    p = 2
    sqrt = n ** 0.5
    while p < sqrt:
        for i in range(2 * p, n, p):
            nums[i] = False
        while True:
            p += 1
            if nums[p]:
                break
    return [i for i in range(n) if nums[i]]
ps = make_ps(105000)
while True:
    n = int(input())
    if not n:
        break
    print(sum(ps[:n]))

Submission status: Accepted

Solution code:
# aoj0053.py

# input: n(<= 100000)
# output: sum of first n primes

LIM = 2000000

isPrime = [True for _ in range(LIM)]

isPrime[0] = isPrime[1] = False

# eratosthenes' sieve
for x in range(LIM):
    if isPrime[x]:
        for y in range(x*x, LIM, x):
            isPrime[y] = False

sumOfPrimes = [x for x in range(LIM) if isPrime[x]]

for i in range(1,len(sumOfPrimes)):
    sumOfPrimes[i] += sumOfPrimes[i-1]

while True:
    n = int(input())
    if n == 0:
        break

    print(sumOfPrimes[n-1])

Submission status: Accepted

Solution code:
primes=[False for i in range(1000000)]
ps=[]
for i in range(2,1000000):
    if not primes[i]:
        ps.append(i)
        for j in range(i,1000000,i):
            primes[j]=True

while True:
    n=int(input())
    if(n==0):break
    sum=0
    for j in range(n):
        sum+=ps[j]
    print(sum)

Submission status: Accepted

Solution code:
prim=[True]*1000000
prim[0],prim[1]=False,False
for i in range(2,1000):
    if prim[i]:
        for j in range(i*2,1000000,i):
            prim[j]=False
prime=[i for i,j in enumerate(prim) if j==True]

while True:
    n=int(input())
    if n==0:
        break
    print(sum(prime[:n]))

Submission status: Accepted

Solution code:
prim=[True]*1000000
prim[0]=prim[1]=False
for i in range(2,350):
    if prim[i]:
        for j in range(i*2,110000,i):
            prim[j]=False
prime=[i for i,j in enumerate(prim) if j==True]

while True:
    n=int(input())
    if n==0:
        break
    print(sum(prime[:n]))

Submission status: Accepted

Solution code:
a=[True]*104743
p=[]
for i in range(2,int(104743**0.5)+1):
    if a[i]:
        for j in range(i*i,104743,i):a[j]=False
for i in range(2,104730):
    if a[i]:p.append(i)
while 1:
    n=int(input())
    if n==0:break
    print(sum(p[:n]))

Submission status: Accepted

Solution code:
def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False

def sieve(n):
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]: mark(s, x)
    return [i for i in range(0,n) if s[i] and i > 1]

n = []
while True:
    tmp = int(input())
    if tmp == 0:
        break
    n.append(tmp)
m = max(n)
p = sieve(500000)

for i in range(len(n)):
    sum = 0
    if n[i] == 1:
        print(2)
    else:
        for j in range(n[i]):
            sum += p[j]
        print(sum)

Submission status: Accepted

Solution code:
is_prime = [1] * 200000
primes = []
for i in range(2,200000):
    if is_prime [i] == False:
        continue
    primes.append(i)
    for j in range(i * 2,200000,i):
        is_prime [j] = False

while True:
    N = int(input())
    if N == 0:
        break
    print(sum(primes [:N]))

Submission status: Accepted

Solution code:
def isPrime(x):
    if x == 2:
        return True
    if x < 2 or x % 2 == 0:
        return False
    i, root_x = 3, int(pow(x, 0.5))
    while i <= root_x:
        if x % i == 0:
            return False
        i += 2
    return True

primes = [2]
for i in range(3, 104730):
    if isPrime(i):
        primes.append(primes[-1]+i)
while True:
    n = int(input())
    if n == 0:
        break
    print(primes[n-1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

def is_prime_list():
    N = 105000
    lst = [True] * N
    lst[0] = False
    lst[1] = False
    for i in range(2, int(N ** 0.5) + 1):
        if lst[i] == True:
            for j in range(2, N):
                index = i * j
                if index < N:
                    lst[i * j] = False
    return [i for i in range(2, N) if lst[i]]


primes = is_prime_list()
for s in sys.stdin:
    n = int(s)
    if n == 0:
        break

    subset = primes[:n]
    print(sum(subset))

Submission status: Accepted

Solution code:
from math import sqrt, ceil, pow

class SieveOfAtkin:
    """ ??¢???????????????
    https://github.com/mccricardo/sieve_of_atkin/blob/master/sieve_of_atkin.py
    """
    def __init__(self, limit):
        self.limit = limit
        self.primes = []
        self.sieve = [False] * (self.limit + 1)

    def flip(self, prime):
        try:
            self.sieve[prime] = True if self.sieve[prime] == False else False
        except KeyError:
            pass

    def invalidate(self, prime):
        try:
            if self.sieve[prime] == True: self.sieve[prime] = False
        except KeyError:
            pass

    def isPrime(self, prime):
        try:
            return self.sieve[prime]
        except KeyError:
            return False

    def getPrimes(self):
        testingLimit = int(ceil(sqrt(self.limit)))

        for i in range(testingLimit):
            for j in range(testingLimit):
                # n = 4*i^2 + j^2
                n = 4 * int(pow(i, 2)) + int(pow(j, 2))
                if n <= self.limit and (n % 12 == 1 or n % 12 == 5):
                    self.flip(n)

                # n = 3*i^2 + j^2
                n = 3 * int(pow(i, 2)) + int(pow(j, 2))
                if n <= self.limit and n % 12 == 7:
                    self.flip(n)

                # n = 3*i^2 - j^2
                n = 3 * int(pow(i, 2)) - int(pow(j, 2))
                if n <= self.limit and i > j and n % 12 == 11:
                    self.flip(n)

        for i in range(5, testingLimit):
            if self.isPrime(i):
                k = int(pow(i, 2))
                for j in range(k, self.limit, k):
                    self.invalidate(j)

        self.primes = [2, 3] + [x for x in range(len(self.sieve)) if self.isPrime(x) and x >= 5]
        return self.primes




if __name__ == '__main__':
    A = SieveOfAtkin(110000)
    A.getPrimes()
    # ??????????????\???
    while True:
        data = int(input())
        if data == 0:
            break

        # ????????¨???????????¨???
        result = A.primes[:data]
        print(sum(result))

Submission status: Accepted

Solution code:
import random

def is_prime(q, k=50):
    """ ??????????????????????´???°?????????????????¨??????????????????????????°????´???°???????????????????????????
    http://d.hatena.ne.jp/pashango_p/20090704/1246692091
    """
    q = abs(q)
    # ?¨???????????????§?????????????????§????????????????????????
    if q == 2: return True
    if q < 2 or q & 1 == 0: return False

    # n-1=2^s*d??¨????????????a?????´??°???d????\???°)???d????±???????
    d = (q - 1) >> 1
    while d & 1 == 0:
        d >>= 1

    # ?????????k?????°?????????
    for i in range(k):
        a = random.randint(1, q - 1)
        t = d
        y = pow(a, t, q)
        # [0,s-1]??????????????????????????§??????
        while t != q - 1 and y != 1 and y != q - 1:
            y = pow(y, 2, q)
            t <<= 1
        if y != q - 1 and t & 1 == 0:
            return False
    return True


Primes = []
def find_nth_prime(n):
    """ n??????????´???°????±??????? """
    count = len(Primes)
    if count >= n:
        return
    if count > 0:
        num = Primes[-1] + 1
    else:
        num = 2
    while True:
        if is_prime(num):
            count += 1
            Primes.append(num)
        if count >= n:
            break
        num += 1


if __name__ == '__main__':
    # ??????????????\???
    while True:
        data = int(input())
        if data == 0:
            break

        # ????????¨???????????¨???
        find_nth_prime(data)
        # print(Primes)
        print(sum(Primes[:data]))

Submission status: Accepted

Solution code:
primes = [0, 0] + [1] * 150000
for i in range(2, 388):
    if primes[i]:
        for j in range(i*i, 150001, i):
            primes[j] = 0

values = [i for i, v in enumerate(primes) if v]
while True:
    n = int(input())
    if n == 0:
        break
    print(sum(values[:n]))

Submission status: Accepted

Solution code:
# Aizu Problem 0053: Sum of Prime Numbers
#
import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def primes2(n):
    """ Input n>=6, Returns a list of primes, 2 <= p < n """
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1,int(n**0.5)//3+1):
      if sieve[i]:
        k=3*i+1|1
        sieve[      k*k//3      ::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
        sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]

primes = primes2(110000)
S = []
s = 0
for p in primes:
    s += p
    S.append(s)

while True:
    n = int(input())
    if n == 0:
        break
    print(S[n-1])

Submission status: Accepted

Solution code:
n = 104730
c = [1 for i in range(n)]
c[0] = 0
i = 2
while i**2 <= n:
    j = i*2
    while j <= n:
        c[j - 1] = 0
        j += i
    i += 1

while True:
    n_i = int(input())
    if n_i == 0:
        break
    s = 0
    i = 0
    j = 0
    while i < n_i:
        j += 1
        if c[j] == 1:
            s += j + 1
            i += 1
    print(s)

Submission status: Accepted

Solution code:
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

MAX = 105000
primes = list()
for i in range(MAX):
    primes.append(True)
primes[0] = False
primes[1] = False

for i in range(2, MAX):
    j = i + i
    while j < MAX:
        primes[j] = False
        j = j + i

primeList = []
for i in range(len(primes)):
    if primes[i]:
        primeList.append(i)

while True:
    n = int(input())
    if n == 0:
        break
    ans = 0
    for i in range(n):
        ans += primeList[i]
    print(ans)

Submission status: Accepted

Solution code:
import itertools
n=104730;a=list(range(n));a[:2]=0,0
for i in range(2,323):
 a[i*2::i]=[0]*len(a[i*2::i])
p=list(itertools.compress(range(n),a))
for e in iter(input,'0'):print(sum(p[:int(e)]))

Submission status: Accepted

Solution code:
import itertools
n=104730;a=list(range(n));a[:2]=0,0
for i in range(2,323):a[i*2::i]=[0]*len(a[i*2::i])
p=list(itertools.compress(range(n),a))
for e in iter(input,'0'):print(sum(p[:int(e)]))

Submission status: Accepted

Solution code:
from itertools import *
n=104730;a=list(range(n));a[:2]=0,0
for i in range(2,323):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(range(n),a))
for e in iter(input,'0'):print(sum(p[:int(e)]))

Submission status: Accepted

Solution code:
import sys
from itertools import *
n=104730;a=list(range(n));a[:2]=0,0
for i in range(2,323):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(range(n),a))
for e in sys.stdin:
 if'0\n'!=e:print(sum(p[:int(e)]))

Submission status: Accepted

Solution code:
from itertools import *
n=104730;a=list(range(n));a[:2]=0,0
for i in range(2,323):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(range(n),a))
print('\n'.join(str(sum(p[:int(e)]))for e in iter(input,'0')))

Submission status: Accepted

Solution code:
from itertools import *
n=104730;a=list(range(n))
for i in range(2,323):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(range(n),a))
for e in iter(input,'0'):print(sum(p[:int(e)+1])-1)

Submission status: Accepted

Solution code:
from itertools import *
n=range(104730);a=list(n)
for i in range(2,323):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(n,a))
for e in iter(input,'0'):print(sum(p[:int(e)+1])-1)

Submission status: Accepted

Solution code:
from itertools import *
n=range(104730);a=list(n)
for i in range(2,320):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(n,a))
for e in iter(input,'0'):print(sum(p[:int(e)+1])-1)

Submission status: Accepted

Solution code:
from itertools import *
n=range(104730);a=list(n)
for i in range(2,321):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(n,a))
for e in iter(input,'0'):print(sum(p[:int(e)+1])-1)

Submission status: Accepted

Solution code:
MAX = 105000

is_prime = [True for _ in range(MAX)]
is_prime[0] = is_prime[1] = False
for i in range(2, int(MAX ** (1 / 2)) + 1):
  if is_prime[i]:
    for j in range(i ** 2, MAX, i):
      is_prime[j] = False

primes = [i for i in range(MAX) if is_prime[i]]

while True:
  n = int(input())
  if not n:
    break
  print(sum(primes[:n]))

Submission status: Accepted

Solution code:
# AOJ 0053: Sum of Prime Numbers
# Python3 2018.6.15 bal4u

MAX = 104729  # 10000th prime
SQRT = 323    # sqrt(MAX)

prime = [True for i in range(MAX+2)]
def sieve():
	for i in range(2, MAX, 2):
		prime[i] = False
	for i in range(3, SQRT, 2):
		if prime[i] is True:
			for j in range(i*i, MAX, i):
				prime[j] = False

sieve()
sum = [0 for i in range(MAX+2)]
j = 2
sum[1] = 2;
for i in range(3, MAX+1, 2):
	if prime[i] is True:
		sum[j] = sum[j-1] + i
		j += 1

while True:
	n = int(input())
	if n == 0: break
	print(sum[n])

Submission status: Accepted

Solution code:
# AOJ 0053: Sum of Prime Numbers
# Python3 2018.6.15 bal4u

MAX = 104729  # 10000th prime
SQRT = 323    # sqrt(MAX)

prime = [True for i in range(MAX+2)]
def sieve():
	for i in range(2, MAX, 2):
		prime[i] = False
	for i in range(3, SQRT, 2):
		if prime[i] is True:
			for j in range(i*i, MAX, i):
				prime[j] = False

sieve()
sum = [0 for i in range(MAX+2)]
j = sum[1] = 2;
for i in range(3, MAX+1, 2):
	if prime[i]:
		sum[j] = sum[j-1] + i
		j += 1

while True:
	n = int(input())
	if n == 0: break
	print(sum[n])

Submission status: Accepted

Solution code:
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]


if __name__ == "__main__":
    all_primes = primes(110000)
    while True:
        n = int(input())
        if n == 0:
            break
        s = sum([all_primes[i] for i in range(n)])
        print(s)

Submission status: Accepted

Solution code:
from itertools import accumulate, chain
ub = 105000
primes = {2, 3} | {m for n in (5, 7) for m in range(n, ub, 6)}
du = primes.difference_update
for n in chain(range(5, ub, 6), range(7, ub, 6)):
    if n in primes:
        du(range(n*3, ub, n*2))

cumsum = tuple(accumulate(primes))

while True:
    n = int(input())
    if n == 0:
        break
    print(cumsum[n-1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

def make_prime_list(lim):
    lst = [True for i in xrange(lim + 1)]
    prime_list = []
    num = 2
    while num ** 2 <= lim:
        for i in xrange(num * 2, lim + 1, num):
            lst[i] = False
        num += 1
        while not lst[num]:
            num += 1
    return [i for i in xrange(2, lim + 1) if lst[i]]

def solve():
    prime_list = make_prime_list(110000)
    accum = [0 for i in prime_list]
    accum[0] = prime_list[0]
    for i in xrange(1, len(accum)):
        accum[i] = accum[i - 1] + prime_list[i]

    while True:
        n = input()
        if n > 0:
            print accum[n - 1]
        else:
            sys.exit()

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
n=15*10**4
p=[True]*n
p[0],p[1]=False,False
for i in xrange(2,int(n**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,n,i):
            p[j]=False
s=[i for i in xrange(n) if p[i]]
while True:
    n=input()
    if n==0:break
    print sum(s[0:n])

Submission status: Accepted

Solution code:
import math
p = [2]
NM = 10000
c = 3
while len(p) < NM:
    m = int(math.sqrt(c))
    r = True
    for i in p:
        if i > m:
            break
        if c % i == 0:
            r = False
            break
    if r:
        p.append(c)
    c += 2

while True:
    n = int(raw_input())
    if n == 0:
        break
    print sum(p[:n])

Submission status: Accepted

Solution code:
import math
NM = 10000
p = [2] * NM
c = 1
a = 3
while c < NM:
    m = int(math.sqrt(a))
    r = True
    for i in p:
        if i > m:
            break
        if a % i == 0:
            r = False
            break
    if r:
        p[c] = a
        c += 1
    a += 2

while True:
    n = int(raw_input())
    if n == 0:
        break
    print sum(p[:n])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin

def enum_prime(limit):
    L = list(range(2, limit + 1))
    P = []
    while L[0] ** 2 < limit:
        P.append(L[0])
        L = [i for i in L if i % P[-1]]
    return P + L

p = enum_prime(104730)
for line in stdin:
    n = int(line)
    if not n:
        break
    print(sum(p[:n]))

Submission status: Accepted

Solution code:
p = [True] * (105000)
p[0] = p[1] = False
for i in range(2, len(p)):
    if p[i]:
        for j in range(i * 2, len(p), i):
            p[j] = False
p = [i for i in range(len(p)) if p[i]]

while True:
    n = int(raw_input())
    if n == 0:
        break
    print sum(p[:n])

Submission status: Accepted

Solution code:
import sys

n = 104729
a = [True] * n
a[0] = False
for i in range(2,n):
	if(a[i-1]):
		for j in range(i**2-1, n, i):
			a[j] = False
a = [i[0]+1 for i in enumerate(a) if i[1]]

for s in sys.stdin:
	n = int(s)
	if n == 0:
		exit()
	print(sum(a[:n]))

Submission status: Accepted

Solution code:
import sys,time

n = 104730
a = [True] * n
a[0] = a[1] = False
for i in range(2,n):
	if(a[i]):
		for j in range(i+i, n, i):
			a[j] = False
a = [i[0] for i in enumerate(a) if i[1]]

for s in sys.stdin:
	n = int(s)
	if n == 0:
		exit()
	print(sum(a[:n]))

Submission status: Accepted

Solution code:
import sys

n = 104730
a = [True] * n
a[0] = a[1] = False
for i in xrange(2,n):
	if(a[i]):
		for j in xrange(i+i, n, i):
			a[j] = False
a = [i[0] for i in enumerate(a) if i[1]]

for s in sys.stdin:
	n = int(s)
	if n == 0:
		exit()
	print(sum(a[:n]))

Submission status: Accepted

Solution code:
s = [0]
M = 10**6
p = [1]*M
S = 0
for i in range(2,M):
    if p[i]:
        for j in range(i*i,M,i):
            p[j] = 0
        S += i; s.append(S)
while 1:
    n = input()
    if n==0: break
    print s[n]

Submission status: Accepted

Solution code:
s = [0]
M = 104850
p = [1]*M
S = 0
for i in range(2,M):
    if p[i]:
        for j in range(i*i,M,i):
            p[j] = 0
        S += i; s.append(S)
while 1:
    n = input()
    if n==0: break
    print s[n]

Submission status: Accepted

Solution code:
import sys

PRIMES=[]
SIEVES=[]
def sieve(max_number):
    numbers = range(1, max_number + 2, 2)
    mroot = int(max_number ** 0.5)+1
    half = len(numbers)
    numbers[0]=0
    for i in range(0, half):
        x = numbers[i]
        if x > mroot: break
        if x and i + x < half:
            numbers[i + x: half: x]=[0] * ((half - 1 - i - x) / x + 1)
    return numbers
def prime(n):
    global SIEVES
    if SIEVES==[]: SIEVES=sieve(n)
    primes =[2] + filter(None, SIEVES[:n/2])
    return primes
PRIMES=prime(105000)
for n in sys.stdin:
    n=int(n)
    if n==0: break
    print sum(PRIMES[:n])