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: Wrong Answer

Solution code:
import math
def pri(n):
    if 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

while True:
    n = int(input())
    if n == 0: break
    sum = 2
    for i in range(3,n+1,2):
        if pri(i): sum += i
    print(sum)

Submission status: Wrong Answer

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) ]

print(len(l))

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

Submission status: Wrong Answer

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


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


p=sieve(10000)
while True:
	n=int(input())
	if n==0:
		break
	print(function(n))

Submission status: Wrong Answer

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

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

p=sieve(10000)
while True:
	n=int(input())
	if n==0:
		break
	print(function(n))

Submission status: Wrong Answer

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

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

p=sieve(10001)
while True:
	n=int(input())
	if n==0:
		break
	print(function(n))

Submission status: Wrong Answer

Solution code:
def sieve(n):
	p=[True for i in range(n+1)]
	p[0]=p[1]=False
	end = int(n**0.5)
	for i in range(2,end+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(p[:n])

p=sieve(10000)
while True:
	n=int(input())
	if n==0:
		break
	print(function(n))

Submission status: Wrong Answer

Solution code:
a=map(int,raw_input().split())
a=a[:-1]
import math
def p(a):
    b,c,i=range(2,int(10+2*math.log(a)*a)),[],0
    while len(c)!=a:
        c.append(b[0])
        b=[i for i in b[1:] if i%b[0]!=0]
    return sum(c)
for i in a:print(str(p(i)))

Submission status: Wrong Answer

Solution code:
import math
def p(a):
    b,c,i=range(2,int(10+2*math.log(a)*a)),[],0
    while len(c)!=a:
        c.append(b[0])
        b=[i for i in b[1:] if i%b[0]!=0]
    return sum(c)
a=map(int,raw_input().split())
for i in a:
    if i!=0:
        print(str(p(i)))
    else:
        break

Submission status: Wrong Answer

Solution code:
def p(a):
    b=[]
    i=1
    while len(b)!=a:
        i+=1
        if ip(i):b.append(i)
    return sum(b)
def ip(a):
    i=2
    while a>=i**2:
        if a%i==0:return False
        i+=1
    return True
a=map(int,raw_input().split())
for i in a:
    if i!=0:
        print(str(p(i)))
    else:
        break

Submission status: Wrong Answer

Solution code:

def is_prime(q):
	q = abs(q)
	if q == 2: return True
	if q < 2 or q&1 == 0: return False
	return pow(2, q-1, q) == 1
					 
if __name__ == "__main__":
	while True:
		x = input()
		if x == 0:
			break
		i, ans = 1, 0
		for j in xrange(x):
			while True:
				i += 1
				if is_prime(i) == True:
					ans += i
					break
				else: pass
		print ans

Submission status: Wrong Answer

Solution code:
def is_prime(q):
	q = abs(q)
	if q == 2: return True
	if q < 2 or q&1 == 0: return False
	return pow(2, q-1, q) == 1
					 
if __name__ == "__main__":
	prime, i = [], 1
	while True:
		i += 1
		if is_prime(i) == True:
			prime.append(i)
		if len(prime) == 10000:
			break

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
prime = [2,3,5,7,11,13,17,19]

shieve1 = [1]*401
for i in prime:
    itr = i-1
    while(itr<400):
        shieve1[itr] = 0
        itr += i

for i in range(20,400):
    if shieve1[i]:
        prime.append(i+1)

shieve2 = [1]*(110001)

for i in prime:
    itr = i-1
    while(itr<110000):
        shieve2[itr] = 0
        itr += i

for i in range(401,110000):
    if shieve2[i]:
        prime.append(i+1)

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

Submission status: Wrong Answer

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

import sys
import os

def is_prime(n):
    if n==2 or n==3: return True
    if n%2==0 or n<2: return False
    for i in range(3,int(n**0.5)+1,2):   # only odd numbers
        if n%i==0:
            return False
    return True

primes = [2]
for i in range(3, 10000+1, 2):
    if is_prime(i):
        primes.append(i)

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

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

Submission status: Wrong Answer

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

import sys
import os

def is_prime(n):
    if n==2 or n==3: return True
    if n%2==0 or n<2: return False
    for i in range(3,int(n**0.5)+1,2):   # only odd numbers
        if n%i==0:
            return False
    return True

primes = [2]
for i in range(3, 10000+1, 2):
    if is_prime(i):
        primes.append(i)
        if len(primes) > 100000:
            break

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

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

Submission status: Wrong Answer

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

import sys
import os

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

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

    if n % 2 == 1:
        if lst[n - 2]:
            print(1)
        else:
            print(0)
    else:
        counter = 0
        for i in range(1, n // 2 + 1, 2):
            prime0 = i
            prime1 = n - i
            if prime0 <= prime1 and lst[prime0] and lst[prime1]:
                counter += 1
        print(counter)

Submission status: Wrong Answer

Solution code:
prime = [2, 3, 5, 7, 11]
while True :
    Sum = 0
    sample = int(input())
    if sample == 0 : break
    elif sample <= len(prime) :
        Sum = sum(prime[:sample])
    else :
        Sum, temp, count = sum(prime), prime[-1], len(prime)
        while True :
            if sample == count : break
            temp += 2
            swith = 0
            if pow(2, temp-1, temp) == 1 :
                Sum += temp
                count += 1
                prime.append(temp)
    print(Sum)

Submission status: Wrong Answer

Solution code:
def is_prime(q):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False
    return pow(2, q-1, q) == 1


def main():
    prime = []
    for i in range(1000000):
        if is_prime(i):
            prime.append(i)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def is_prime(q):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False
    return pow(2, q-1, q) == 1


def main():
    prime = []
    for i in range(104000):
        if is_prime(i):
            prime.append(i)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
def is_prime(q):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False
    return pow(2, q-1, q) == 1


def main():
    prime = []
    for i in range(104000):
        if is_prime(i):
            prime.append(i)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
n=10001;a=list(range(n));a[:2]=0,0
for i in range(2,97):
 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: Wrong Answer

Solution code:
import itertools
n=104730;a=list(range(n));a[:2]=0,0
for i in range(2,97):
 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: Wrong Answer

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))
print(len(p))
for e in iter(input,'0'):print(sum(p[:int(e)]))

Submission status: Wrong Answer

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'!=e:print(sum(p[:int(e)]))

Submission status: Wrong Answer

Solution code:
from itertools import *
n=range(104730);a=list(n)
for i in range(2,300):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: Wrong Answer

Solution code:
from itertools import *
n=range(104730);a=list(n)
for i in range(2,310):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: Wrong Answer

Solution code:
from itertools import *
n=range(104730);a=list(n)
for i in range(2,315):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: Wrong Answer

Solution code:
import sys
n=10**5+1
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: Wrong Answer

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]]
print len(s)
while True:
    n=input()
    if n==0:break
    print sum(s[0:n])

Submission status: Wrong Answer

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(10000)
for line in stdin:
    n = int(line)
    if not n:
        break
    print(sum(p[:n]))

Submission status: Wrong Answer

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:
	if s == '0':
		exit()
	print(sum(a[:int(s)]))

Submission status: Wrong Answer

Solution code:
import math
r = 10000
sqrt = int(math.sqrt(r))
p = [1]*r
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        for j in range(2*i+1,r,i+1):
            p[j] = 0

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

Submission status: Wrong Answer

Solution code:
import math

r = 105000
sqrt = int(math.sqrt(r))
prime = []
p = [1]*r
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        prime.append(i+1)
        for j in range(2*i+1,r,i+1):
            p[j] = 0

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

Submission status: Wrong Answer

Solution code:
import math

r = 105000
sqrt = int(math.sqrt(r))
prime = []
p = [1]*r
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        prime.append(i+1)
        for j in range(2*i+1,r,i+1):
            p[j] = 0

while True:
    n = int(raw_input())
    if not n:
        break
    print sum(prime[:n])

Submission status: Wrong Answer

Solution code:
import math
 
r = 105000
sqrt = int(math.sqrt(r))
p = [1]*r
prime = []
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        prime.append(i+1)
        for j in range(2*i+1,r,i+1):
            p[j] = 0
 
while True:
    n = int(raw_input())
    if not n:
        break
    print sum(prime[:n])

Submission status: Wrong Answer

Solution code:
import math
 
prime = [0 for i in range(11000)]
k = 0
r = 105000
sqrt = int(math.sqrt(r))
p = [1]*r
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        prime[k] = i+1
        k += 1
        l = i
        for j in range(2*i+1,r,i+1):
            p[j] = 0


for i in range(l,r):
    if p[i]:
        prime[k] = i+1
        k += 1
        
while True:
    n = int(raw_input())
    if not n:
        break
    print sum(prime[:n])

Submission status: Wrong Answer

Solution code:
import math
 
prime = [0 for i in range(11000)]
k = 0
r = 104000
sqrt = int(math.sqrt(r))
p = [1]*r
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        prime[k] = i+1
        k += 1
        l = i
        for j in range(2*i+1,r,i+1):
            p[j] = 0


for i in range(l+1,r):
    if p[i]:
        prime[k] = i+1
        k += 1
        
while True:
    n = int(raw_input())
    if not n:
        break
    print sum(prime[:n])

Submission status: Wrong Answer

Solution code:
import math
 
prime = [0 for i in range(11000)]
k = 0
r = 104500
sqrt = int(math.sqrt(r))
p = [1]*r
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        prime[k] = i+1
        k += 1
        l = i
        for j in range(2*i+1,r,i+1):
            p[j] = 0


for i in range(l+1,r):
    if p[i]:
        prime[k] = i+1
        k += 1
        
while True:
    n = int(raw_input())
    if not n:
        break
    print sum(prime[:n])

Submission status: Wrong Answer

Solution code:
import math
 
prime = [0 for i in range(11000)]
k = 0
r = 104700
sqrt = int(math.sqrt(r))
p = [1]*r
p[0] = 0
for i in range(1,sqrt):
    if p[i]:
        prime[k] = i+1
        k += 1
        l = i
        for j in range(2*i+1,r,i+1):
            p[j] = 0


for i in range(l+1,r):
    if p[i]:
        prime[k] = i+1
        k += 1
        
while True:
    n = int(raw_input())
    if not n:
        break
    print sum(prime[:n])

Submission status: Wrong Answer

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

prime_list = sieve(10001)
while 1:
    a = input()
    if a == 0:
        break
    print sum(prime_list[:a])

Submission status: Time Limit Exceeded

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]]


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

Submission status: Time Limit Exceeded

Solution code:
def p(a):
    b=[]
    i=1
    while len(b)!=a:
        i+=1
        if ip(i):b.append(i)
    return sum(b)
def ip(a):
    i=2
    while a>=i**2:
        if a%i==0:return False
        i+=1
    return True
while True:
    a=input
    if a!=0:
        print(str(p(a)))
    else:
        break

Submission status: Time Limit Exceeded

Solution code:
def p(a):
    b=[]
    i=1
    while len(b)!=a:
        i+=1
        if ip(i):b.append(i)
    return sum(b)
def ip(a):
    i=2
    while a>=i**2:
        if a%i==0:return False
        i+=1
    return True
while True:
    a=input()
    if a!=0:
        print(str(p(a)))
    else:
        break

Submission status: Time Limit Exceeded

Solution code:
def p(a):
    b=[]
    i=1
    while len(b)!=a:
        i+=1
        if ip(i):b.append(i)
    return sum(b)
def ip(a):
    i=2
    while a>=i**2:
        if a%i==0:return False
        i+=1
    return True
t=True
while t:
    a=input()
    if a!=0:
        print(str(p(a)))
    else:
        t=False

Submission status: Time Limit Exceeded

Solution code:
def p(a):
    b=[]
    i=1
    while len(b)!=a:
        i+=1
        if ip(i):b.append(i)
    return sum(b)
def ip(a):
    i=2
    while a>=i**2:
        if a%i==0:return False
        i+=1
    return True
while True:
    a=input()
    if a==0:
        break
    else:
        print(str(p(a)))

Submission status: Time Limit Exceeded

Solution code:
def p(a):
    b=[]
    i=1
    while len(b)!=a:
        i+=1
        if ip(i):b.append(i)
    return sum(b)
def ip(a):
    i=2
    while a>=i**2:
        if a%i==0:return False
        i+=1
    return True
while True:
    a=input()
    if a==0:
        break
    else:
        print(str(p(int(a))))

Submission status: Time Limit Exceeded

Solution code:
def p(a):
    b=[]
    i=1
    while len(b)!=a:
        i+=1
        if ip(i):b.append(i)
    return sum(b)
def ip(a):
    i=2
    while a>=i**2:
        if a%i==0:return False
        i+=1
    return True
while True:
    a=int(input())
    if a==0:
        break
    else:
        print(str(p(a)))

Submission status: Time Limit Exceeded

Solution code:
import sys

def gen1():
  yield 2
  l = [2]
  count = 2
  while True:
    count += 1
    flag = 1
    for i in l:
      if i > count ** (1/2):
        break
      else:
        if count % i == 0:
          flag = 0
          break
    if flag:
      l.append(count)
      yield count

for line in sys.stdin:
  g = gen1()
  n = int(line)
  if n == 0:
    break
  s = 0
  for i in range(n):
    a = g.__next__()
    s += a
    print(a)
  print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
def isprime(p):
    if p == 2:
        return(True)
    if p % 2 ==0:
        return(False)
    for i in range(3,math.floor(math.sqrt(p))+1,2):
        if p % i == 0:
            return(False)
    return(True) 

def takenp(n):
    p = [2]
    if n == 1:
        return(p)
    for i in range(3,1000000,2):    # p(10000) is 104729
        if isprime(i):
            p.append(i)
            if len(p) == n:
                return(p)
         
if __name__ == '__main__':
    while True:
        n = int(input().strip())
        if n==0:
            break
        print(sum(takenp(n)))

Submission status: Time Limit Exceeded

Solution code:
import math
def isprime(p):
    if p == 2:
        return(True)
    if p % 2 ==0:
        return(False)
    for i in range(3,math.floor(math.sqrt(p))+1,2):
        if p % i == 0:
            return(False)
    return(True) 

def takenp(n):
    p = [2]
    if n == 1:
        return(p)
    for i in range(3,1000000,2):    # p(10000) is 104729
        if isprime(i):
            p.append(i)
            if len(p) == n:
                return(p)
         
if __name__ == '__main__':
    f = takenp(10000)
    s = [2]
    for i in range(1,10000):
        s.append(s[i-1]+f[i])

    while True:
        n = int(input().strip())
        if n==0:
            break
        print(sum(takenp(n)))

Submission status: Time Limit Exceeded

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

import sys
import os

def is_prime(n):
    if n==2 or n==3: return True
    if n%2==0 or n<2: return False
    for i in range(3,int(n**0.5)+1,2):   # only odd numbers
        if n%i==0:
            return False
    return True

primes = [2]
i = 3
while True:
    if is_prime(i):
        primes.append(i)
        if len(primes) > 100000:
            break
    i += 2

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

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

Submission status: Time Limit Exceeded

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

import sys
import os

def is_prime(n):
    if n==2 or n==3: return True
    if n%2==0 or n<2: return False
    for i in range(3,int(n**0.5)+1,2):   # only odd numbers
        if n%i==0:
            return False
    return True

def make_primes():
    primes = [2]
    i = 3
    while True:
        if is_prime(i):
            primes.append(i)
            if len(primes) > 105000:
                break
        i += 2
    return primes

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

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

Submission status: Time Limit Exceeded

Solution code:
from math import sqrt

while True :
    sample = int(input())
    if sample == 0 : break
    Sum, count, number = 2, 1, 1
    while True :
        if count == sample : break
        if 2 < number :
            temp, switch = 3, 0
            while temp <= sqrt(number) :
                if number % temp == 0 : 
                    switch = 1
                    break
                temp += 2
            if switch != 1 : 
                Sum, count = Sum+number, count+1
        number += 2
    print(Sum)

Submission status: Time Limit Exceeded

Solution code:
prime = [2, 3]
while True :
    Sum = 0
    sample = int(input())
    if sample == 0 : break
    elif sample <= len(prime) :
        Sum = sum(prime[:sample])
    else :
        Sum, temp, count = sum(prime), prime[-1], len(prime)
        while True :
            if sample == count : break
            temp += 2
            swith = 0
            for _ in prime :
                if temp % _ == 0 :
                    swith = 1
                    break
            if swith == 0 : 
                Sum += temp
                count += 1
                prime.append(_)
    print(Sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
prime = deque([2, 3, 5, 7, 11])
while True :
    Sum = 0
    sample = int(input())
    if sample == 0 : break
    elif sample <= len(prime) :
        for _ in range(sample) :
            Sum += prime[_]
    else :
        Sum, temp, count = sum(prime), prime[-1], len(prime)
        while True :
            if sample == count : break
            temp += 2
            swith = 0
            for _ in prime :
                if temp % _ == 0 :
                    swith = 1
                    break
            if swith == 0 : 
                Sum += temp
                count += 1
                prime.append(_)
    print(Sum)

Submission status: Time Limit Exceeded

Solution code:
def is_prime(q, k=50):
    """ ??????????????????????´???°?????????????????¨??????????????????????????°????´???°???????????????????????????
    http://d.hatena.ne.jp/pashango_p/20090704/1246692091
    """
    import random
    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


def find_nth_prime(n):
    """ n???????????§????´???°????±??????? """
    count = 0
    num = 2
    result = []
    while True:
        if is_prime(num):
            count += 1
            result.append(num)
        if count >= n:
            break
        num += 1
        #print(count, num)
    return result


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:
def create_prime_list(limit):
    """ ??¨??????????????????????????§limit?????§????´???°?????????????±???????
    https://ja.wikipedia.org/wiki/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9
    """
    x = limit**0.5
    primes = []
    #print('x={0}'.format(x))
    nums = [x for x in range(2, limit+1)]
    while nums[0]<=x:
        primes.append(nums[0])
        current_prime = nums[0]
        nums = [x for x in nums if x%current_prime != 0]
    primes.extend(nums)
    #print(primes)
    return primes


def solve(n):
    primes = create_prime_list(110000)
    primes = primes[:n]
    return primes


def is_prime(q, k=50):
    """ ??????????????????????´???°?????????????????¨??????????????????????????°????´???°???????????????????????????
    http://d.hatena.ne.jp/pashango_p/20090704/1246692091
    """
    import random
    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


def find_nth_prime(n):
    """ n???????????§????´???°????±??????? """
    count = 0
    num = 2
    result = []
    while True:
        if is_prime(num):
            count += 1
            result.append(num)
        if count >= n:
            break
        num += 1
        #print(count, num)
    print(result)
    return result


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

        # ????????¨???????????¨???
        # result = find_nth_prime(data)
        result = solve(data)

        print(sum(result))

Submission status: Time Limit Exceeded

Solution code:
def create_prime_list(limit):
    """ ??¨??????????????????????????§limit?????§????´???°?????????????±???????
    https://ja.wikipedia.org/wiki/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9
    """
    x = limit**0.5
    primes = []
    #print('x={0}'.format(x))
    nums = [x for x in range(2, limit+1)]
    while nums[0]<=x:
        primes.append(nums[0])
        current_prime = nums[0]
        nums = [x for x in nums if x%current_prime != 0]
    primes.extend(nums)
    #print(primes)
    return primes


def solve(n):
    primes = create_prime_list(104729)
    primes = primes[:n]
    return primes


def is_prime(q, k=50):
    """ ??????????????????????´???°?????????????????¨??????????????????????????°????´???°???????????????????????????
    http://d.hatena.ne.jp/pashango_p/20090704/1246692091
    """
    import random
    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


def find_nth_prime(n):
    """ n???????????§????´???°????±??????? """
    count = 0
    num = 2
    result = []
    while True:
        if is_prime(num):
            count += 1
            result.append(num)
        if count >= n:
            break
        num += 1
        #print(count, num)
    print(result)
    return result


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

        # ????????¨???????????¨???
        # result = find_nth_prime(data)
        result = solve(data)

        print(sum(result))

Submission status: Time Limit Exceeded

Solution code:
def is_prime(n):
    if n == 2:
        return True
    if n < 2 or (n%2 == 0):
        return False
    i = 3
    while i <= n**0.5:
        if n % i == 0:
            return False
        i += 2
    return True


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


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

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