Problem p03362 (python)

Fixed submission:
import math

def primes(x):
    if x < 2: return []
 
    primes = [i for i in range(x)]
    primes[1] = 0 # 1は素数ではない
 
    # エラトステネスのふるい
    for prime in primes:
        if prime > math.sqrt(x): break
        if prime == 0: continue
        for non_prime in range(2 * prime, x, prime): primes[non_prime] = 0
 
    return [prime for prime in primes if prime != 0]
  
n = int(input())
prime_nums = primes(55555)
ans = []
for p in prime_nums:
  if str(p)[-1] == '1':
    ans.append(p)
  if len(ans) == n:
    for a in ans:
      print(a, end = ' ')
    quit()

Buggy submission (verdict: Runtime Error):
import math
from itertools import combinations
import sys
def primes(x):
    if x < 2: return []
 
    primes = [i for i in range(x)]
    primes[1] = 0 # 1は素数ではない
 
    # エラトステネスのふるい
    for prime in primes:
        if prime > math.sqrt(x): break
        if prime == 0: continue
        for non_prime in range(2 * prime, x, prime): primes[non_prime] = 0
 
    return [prime for prime in primes if prime != 0]
  
def is_prime(x):
    if x < 2: return False # 2未満に素数はない
    if x == 2 or x == 3 or x == 5: return True # 2,3,5は素数
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0: return False # 2,3,5の倍数は合成数
 
    # ためし割り: 疑似素数(2でも3でも5でも割り切れない数字)で次々に割っていく
    prime = 7
    step = 4
    while prime <= math.sqrt(x):
        if x % prime == 0: return False
 
        prime += step
        step = 6 - step
 
    return True
  
prime_nums = primes(55555)
prime_list = prime_nums[:5]
prime_remain = prime_nums[5:]
N = int(input())
for p in prime_remain:
  prime_l = prime_list + [p]
  combs = list(combinations(prime_l, 5))
  if not True in [is_prime(sum(c)) for c in combs]:
    prime_list = [p for p in prime_l]
  
  # print(len(prime_list), p)
  if len(prime_list) == N:
    for p in prime_list:
      print(p, end = ' ')
    break
    # sys.exit()
import math
from itertools import combinations
import sys
def primes(x):
    if x < 2: return []

    primes = [i for i in range(x)]
    primes[1] = 0 # 1は素数ではない

    # エラトステネスのふるい
    for prime in primes:
        if prime > math.sqrt(x): break
        if prime == 0: continue
        for non_prime in range(2 * prime, x, prime): primes[non_prime] = 0

    return [prime for prime in primes if prime != 0]
  
def is_prime(x):
    if x < 2: return False # 2未満に素数はない
    if x == 2 or x == 3 or x == 5: return True # 2,3,5は素数
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0: return False # 2,3,5の倍数は合成数

    # ためし割り: 疑似素数(2でも3でも5でも割り切れない数字)で次々に割っていく
    prime = 7
    step = 4
    while prime <= math.sqrt(x):
        if x % prime == 0: return False

        prime += step
        step = 6 - step

    return True
  
prime_nums = primes(55555)
ans = ''
for p in prime_nums:
  if str(p)[-1] == '1':
    ans += str(p) + ' '
print(ans)

'''
prime_list = prime_nums[:5]
prime_remain = prime_nums[5:]
N = int(input())
for p in prime_remain:
  prime_l = prime_list + [p]
  combs = list(combinations(prime_l, 5))
  if not True in [is_prime(sum(c)) for c in combs]:
    prime_list = [p for p in prime_l]
  
  # print(len(prime_list), p)
  if len(prime_list) == N:
    for p in prime_list:
      print(p, end = ' ')
    break
    # sys.exit()