Problem:


四つ子素数

  (a, a+2, a+6, a+8) のように並んだ 4 つの素数の組を四つ子素数といいます。四つ子素数を構成する四つの素数のうち、最大の数をその四つ子素数の大きさと呼びます。例えば、最も小さい大きさの四つ子素数は、(5, 7, 11, 13) の組であり、その大きさは 13 です。次に大きい四つ子素数は、(11, 13, 17, 19) の組で、その大きさは 19 です。

  整数 n (13 ≤ n ≤ 10,000,000) を入力とし、大きさが n 以下になるような四つ子素数のうち、最大となるものの大きさを出力するプログラムを作成してください。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットとして１つの整数 n が１行に与えられます。

データセットの数は 2000 を超えません。

Output

入力データセットごとに、最大となる四つ子素数の大きさを1行に出力します。

Sample Input

13
14
15
16
17
18
19
20
10000
0

Output for the Sample Input

13
13
13
13
13
13
19
19
9439



Sample input:
13
14
15
16
17
18
19
20
10000
0


Sample output:
13
13
13
13
13
13
19
19
9439

Submission status: Wrong Answer

Solution code:

def is_prime2(q,k=100):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False
    for i in xrange(3,k):
        x,y = q,i
        while y:
            x, y =  y, x % y
        if x != 1: continue
        if pow(i, q-1, q) != 1:
            return False
    return True

while True:
    n = input()
    break
    if (n == 0):
        break
    if (n < 19):
        print 13
    else:
        tmp = int((n - 19) / 30)
        while tmp > 0:
            x = 30 * tmp + 19
            if (is_prime2(x) and is_prime2(x - 2) and is_prime2(x - 6) and is_prime2(x - 8)):
                print x
                break
            tmp -= 1

Submission status: Wrong Answer

Solution code:

def is_prime2(q,k=100):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False
    for i in xrange(3,k):
        x,y = q,i
        while y:
            x, y =  y, x % y
        if x != 1: continue
        if pow(i, q-1, q) != 1:
            return False
    return True

while True:
    n = input()
    if (n == 0):
        break
    if (n < 19):
        print 13
    else:
        print "hoge"
        break
        tmp = int((n - 19) / 30)
        while tmp > 0:
            x = 30 * tmp + 19
            if (is_prime2(x) and is_prime2(x - 2) and is_prime2(x - 6) and is_prime2(x - 8)):
                print x
                break
            tmp -= 1

Submission status: Wrong Answer

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


prime = sieve()

print()

Submission status: Wrong Answer

Solution code:

while True:
    ni = input().strip()
    if ni == '0':
        break
    print('0')

Submission status: Time Limit Exceeded

Solution code:
from math import sqrt, ceil

N = 10100000
temp = [True]*(N+1)
temp[0] = temp[1] = False
for i in range(2, ceil(sqrt(N+1))):
    if temp[i]:
        temp[i+i::i] = [False]*(len(temp[i+i::i]))

quadruplet = [True, False, True, False, False, False, True, False,True]
ans = [0]*N
for i in range(8,N):
    if temp[i] and temp[i-8:i+1] == quadruplet: ans[i] = i
while True:
    n = int(input())
    if not n: break
    print(max(ans[:n+1]))

Submission status: Time Limit Exceeded

Solution code:
import sys

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:
        print "hoge"
    while True:
        n = input()
        if (n == 0):
            break
        if (n < 19):
            print 13
        else:
            tmp = int((n - 19) / 30)
            if tmp == 0:
                print 19
            while tmp > 0:
                x = 30 * tmp + 19
                if (is_prime(x) and is_prime(x - 2) and is_prime(x - 6) and is_prime(x - 8)):
                    print x
                    break
                tmp -= 1

Submission status: Time Limit Exceeded

Solution code:
def make_sosu_tbl():
    for i in range(2, MAX_N):
        if sos[i]:
            for j in range(i+i, MAX_N+1, i):
                sos[j] = 0
        if i**2 >= MAX_N:break

while 1:
    MAX_N = int(input())
    if not MAX_N: break
    sos = [1] * (MAX_N+1)
    make_sosu_tbl()

    for i in reversed(range(13,MAX_N+1)):
        if sos[i] and sos[i-2] and sos[i-6] and sos[i-8]:
            print(i)
            break

Submission status: Time Limit Exceeded

Solution code:
MAX_N = 10000000+1

sos = [1] * (MAX_N+1)
for i in range(2, MAX_N):
    if sos[i]:
        for j in range(i+i, MAX_N+1, i):
            sos[j] = 0
    if i**2 >= MAX_N:break

while 1:
    try:
        n = int(input())

        if not n: break
        for i in reversed(range(13, n+1)):
            if sos[i] and sos[i-2] and sos[i-6] and sos[i-8]:
                print(i)
                break
    except:
        pass

Submission status: Time Limit Exceeded

Solution code:
MAX_N = 10000000+1
SQRT_MAX = 3163
sos = [1] * (MAX_N+1)
for i in range(2, SQRT_MAX+1):
    if sos[i]:
        for j in range(i+i, MAX_N+1, i):
            sos[j] = 0

while 1:
    try:
        n = int(input())

        if not n: break
        for i in reversed(range(13, n+1)):
            if sos[i] and sos[i-2] and sos[i-6] and sos[i-8]:
                print(i)
                break
    except:
        pass

Submission status: Time Limit Exceeded

Solution code:
# from datetime import datetime

# measure_start = datetime.now()

MAX_N = 10000000+1
sos = [1] * (MAX_N+1)
sos[0] = sos[1] = 0
for i in range(2, int(MAX_N**0.5)+1):
    if sos[i]:
        for j in range(i+i, MAX_N+1, i):
            sos[j] = 0

while 1:
    try:
        n = int(input())

        if not n: break
        n -= (n%2 == 1)
        for i in reversed(range(13, n+1)):
            if sos[i] and sos[i-2] and sos[i-6] and sos[i-8]:
                print(i)
                break
    except:
        pass
# measure_stop = datetime.now()
# print(measure_stop - measure_start)

Submission status: Time Limit Exceeded

Solution code:
def get_quad(n, p):
    for ni in range(n,0,-1):
        if p[ni] and p[ni - 2] and p[ni - 6] and p[ni - 8]:
            return ni
        
def sieve():
    n = 10000001
    p = [1] * n
    p[0] = p[1] = 0
    
    for i in range(int(n ** 0.5)):
        if p[i]:
            for j in range(2 * i, len(p), i):
                p[j] = 0
    return p
import sys
f = sys.stdin

prime = sieve()
while True:
    n = f.readline()
    if not n.isnumeric():
        continue
    n = int(n)
    if n == 0:
        break
    print(get_quad(ni, prime))

Submission status: Time Limit Exceeded

Solution code:
import math
import bisect

primes=[2]
top=2
nlist=[2*x-1 for x in range(2, 10000000/2)]
while top<math.sqrt( 10000000):
    top=nlist[0]
    primes.append(top)
    newnlist=[]
    for i in nlist:
        if i%top!=0:
            newnlist.append(i)
    nlist=newnlist
primes=primes+nlist
#print (primes)
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        i=bisect.bisect(primes,n)
        while(1):
            i=i-1
            if primes[i]==primes[i-1]+2:
                if primes[i-1]==primes[i-2]+4:
                    if primes[i-2]==primes[i-3]+2:
                        print primes[i]
                        break

Submission status: Time Limit Exceeded

Solution code:
n=10000000
p=[1]*(n+1)
p[0],p[1]=0,0
for i in range(2,int(n**0.5)+1):
    if p[i]:        
        for j in range(i*i,n+1,i):
            p[j]=0
#p=[i for i in range(n+1) if p[i]==1]
while 1:
    n=int(input())
    if n==0: break
    for i in range(n,2,-1):
        if (p[i],p[i-2],p[i-6],p[i-8])==(1,1,1,1):
            print(i)
            break

Submission status: Time Limit Exceeded

Solution code:
primes = [0, 0] + [1]*9999999
for i in range(2, 3163):
    for j in range(i*i, 10000001, i):
        primes[j] = 0

while True:
    n = int(input())
    if n == 0:
        break
    for i in range(5, n+1)[::-1]:
        if primes[i] and primes[i-2] and primes[i-6] and primes[i-8]:
            print(i)
            break

Submission status: Time Limit Exceeded

Solution code:
primes = [0, 0] + [1] * 9999999
for i in range(2, 3163):
    for j in range(i*i, 10000000, i):
        if primes[j]:
            primes[j] = 0

while True:
    n = int(input())
    if n == 0:
        break
    while True:
        if primes[n-8] and primes[n-6] and primes[n-2] and primes[n]:
            print(n)
            break
        else:
            n -= 1

Submission status: Time Limit Exceeded

Solution code:
primes = [0, 0] + [1] * 9999999
for i in range(2, 3163):
    if primes[i]:
        for j in range(i*i, 10000001, i):
            primes[j] = 0

while True:
    n = int(input())
    if n == 0:
        break
    while not (primes[n-8] and primes[n-6] and primes[n-2] and primes[n]):
        n -= 1
    print(n)

Submission status: Time Limit Exceeded

Solution code:
primes = [0, 0] + [1] * 9999999
for i in range(2, 3163):
    if primes[i]:
        for j in range(i*i, 10000001, i):
            primes[j] = 0

while True:
    n = int(input())
    if n == 0:
        break
    while not (primes[n-8] & primes[n-6] & primes[n-2] & primes[n]):
        n -= 1
    print(n)

Submission status: Time Limit Exceeded

Solution code:
primes = [0, 0] + [1] * 9999999
for i in range(2, 3163):
    if primes[i]:
        for j in range(i*i, 10000001, i):
            primes[j] = 0

while True:
    n = int(input())
    if n == 0:
        break
    for i in range(5, n+1)[::-1]:
        if primes[i-8] & primes[i-6] & primes[i-2] & primes[i]:
            print(i)
            break

Submission status: Time Limit Exceeded

Solution code:
primes = [0, 0] + [1] * 9999999
for i in range(2, 3163):
    if primes[i]:
        for j in range(i*i, 10000001, i):
            primes[j] = 0

while True:
    n = int(input())
    if n == 0:
        break
    while not (primes[n] & primes[n-2] & primes[n-6] & primes[n-8]):
        n -= 1
    print(n)

Submission status: Time Limit Exceeded

Solution code:
def p(n):
	if n%2==0:
		return False
	for i in xrange(3,int(n**0.5)+1,2):
		if n%i==0:
			return False
	else:
		return True
		
while 1:
	n=input()
	if n==0:break
	for i in xrange(n,11,-1):
		if p(i) and p(i-2) and p(i-6) and p(i-8):
			print i
			break

Submission status: Time Limit Exceeded

Solution code:
r=10000000
sqrt=int(r**0.5)
p=[1]*r
p[0]=0
p[3::2]=[0 for i in xrange(3,r,2)]
for i in xrange(2,sqrt,2):
	if p[i]:
		p[2*i+1::i+1]=[0 for x in range(2*i+1,r,i+1)]

while 1:
	n=input()
	if n==0:break
	for i in xrange(n-1,11,-1):
		if p[i] and p[i-2] and p[i-4] and p[i-6]:
			print i+1
			break

Submission status: Time Limit Exceeded

Solution code:
def isP(n):
	for i in prime:
		if i >= n:
			break
		if n%i == 0:
			return False
	return True
		
r = int(10000001**0.5)
s = int(r**0.5)
p = [1]*r
p[0] = p[1] = 0
for i in range(2,s):
	if p[i]:
		p[2*i::i] = [0 for x in range(2*i,r,i)]
prime = []
for i in range(r):
	if p[i]:
		prime.append(i)

while 1:
	n = input()
	if n == 0: break
	for i in range(n-(1-n%2),10,-2):
		if isP(i) and isP(i-2) and isP(i-6) and isP(i-8):
			print i
			break

Submission status: Time Limit Exceeded

Solution code:
def isP(n):
	for i in prime:
		if i >= n:
			break
		if n%i == 0:
			return False
	return True

r = int(10000001**0.5)
s = int(r**0.5)
p = [1]*r
p[0] = p[1] = 0
for i in range(2,s):
	if p[i]:
		p[2*i::i] = [0 for x in range(2*i,r,i)]
prime = []
for i in range(r):
	if p[i]:
		prime.append(i)

while 1:
	try:
		n = input()
		if n == 0: break
		i = n-(1-n%2)
		while i > 12:
			p6 = isP(i-6)
			if not isP(i-8) or not isP(i-2):
				if not p6:
					i -= 10
				else:
					i -= 4
				continue
			if isP(i) and p6:
				print i
				break
			i -= 2
	except:
		pass

Submission status: Time Limit Exceeded

Solution code:
def isP(n):
	for i in prime:
		if i >= n:
			break
		if n%i == 0:
			return False
	return True

r = int(10000001**0.5)
s = int(r**0.5)
p = [1]*r
p[0] = p[1] = 0
for i in range(2,s):
	if p[i]:
		p[2*i::i] = [0 for x in range(2*i,r,i)]
prime = []
for i in range(r):
	if p[i]:
		prime.append(i)

while 1:
	try:
		n = input()
		if n == 0: break
		i = n-(1-n%2)
		while i > 12:
			p6 = isP(i-6)
			if not isP(i-8) or not isP(i-2):
				if not p6:
					i -= 10
				else:
					i -= 4
				continue
			if isP(i) and p6:
				print i
				break
			i -= 2
	except:
		break

Submission status: Time Limit Exceeded

Solution code:
r =10000001
p = [1]*r
p[0]= p[1] = 0
for i in range(int(r**0.5)):
	if p[i]:
		p[2*i::i] = [0 for j in range(2*i,r,i)]
		
while 1:
	try:
		n = input()
		if n == 0: break
		n -= 1-n%2
		while any(not p[n-i] for i in [0,2,6,8]):
			n -= 2
		print n
	except:
		pass

Submission status: Memory Limit Exceeded

Solution code:
while True:
    N = input()
    if not N:
        break
    prime = [0, 0, 1] + [-1] * (N + 10)
    for i in range(2, N + 1):
        if prime[i] != -1:
            continue
        prime[i] = 1
        for j in range(i + i , N + 1, i):
            prime[j] = 0

    n = 0
    a = 0
    for i in range(8, N + 1):
        print(i, prime[i - 8], prime[i - 6], prime[i - 2], prime[i])
        if prime[i - 8] and prime[i - 6] and prime[i - 2] and prime[i]:
            a = max(a, i)
    print(a)

Submission status: Memory Limit Exceeded

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:
        n = input()
        if (n == 0):
            break
        if (n < 19):
            print 13
        else:
            for x in range(n, 13, -1):
                if (is_prime(x) and is_prime(x - 2) and is_prime(x - 6) and is_prime(x - 8)):
                    print x
                    break

Submission status: Memory Limit Exceeded

Solution code:
import math
def sieve(n):
    sieve = [1 for i in range(n + 1)]
    sieve[0] = 0
    sieve[1] = 0
    for i in range(2, int(math.sqrt(n)) + 1):
        if sieve[i] == 1:
            for j in range(2 * i, n, i):
                sieve[j] = 0
    return sieve

while True:
    n = input()
    if n == 0:
        break
    prime = sieve(n)
    for i in range(n, 0, - 1):
        if (prime[i] and prime[i - 2] and prime[i - 6] and prime[i - 8]):
            print i
            break

Submission status: Memory Limit Exceeded

Solution code:
import math
N=10000000
prime=[1]*(N+1)
prime[0],prime[1]=0,0
for i in range(2,int(math.sqrt(N))):
    if prime[i]==1:
        j=i
        while i*j<=N:
            prime[i*j]=0
            j+=1

while True:
    n=input()
    if n==0:break
    for i in range(n,2,-1):
        if prime[i]==prime[i-2]==prime[i-6]==prime[i-8]==1:
            print i
            break

Submission status: Memory Limit Exceeded

Solution code:
r=10000000
sqrt=int(r**0.5)
p=[1]*r
p[0]=0
p[3::2]=[0 for i in range(3,r,2)]
for i in range(2,sqrt,2):
	if p[i]:
		p[2*i+1::i+1]=[0 for x in range(2*i+1,r,i+1)]

while 1:
	n=input()
	if n==0:break
	for i in range(n-1,11,-1):
		if p[i] and p[i-2] and p[i-6] and p[i-8]:
			print i+1
			break

Submission status: Memory Limit Exceeded

Solution code:
def p(n):
	for i in range(2,int(n**0.5)+1):
		if n%i==0:
			return False
	else:
		return True
		
while 1:
	n=input()
	if n==0:break
	for i in range(n,11,-1):
		if p(i) and p(i-2) and p(i-6) and p(i-8):
			print i
			break

Submission status: Memory Limit Exceeded

Solution code:
r = 10000001
s = int(r**0.5)
p = [1]*r
p[0] = p[1] = 0
for i in range(2,s):
	if p[i]:
		p[2*i::i] = [0 for x in range(2*i,r,i)]

while 1:
	n = input()
	if n == 0: break
	for i in range(n-(1-n%2),12,-1):
		if p[i] and p[i-2] and p[i-6] and p[i-8]:
			print i
			break

Submission status: Accepted

Solution code:
from math import sqrt, ceil

N = 10100000
temp = [True]*(N+1)
temp[0] = temp[1] = False
for i in range(2, ceil(sqrt(N+1))):
    if temp[i]:
        temp[i+i::i] = [False]*(len(temp[i+i::i]))

quadruplet = [True, False, True, False, False, False, True, False,True]
while True:
    try: n = int(input())
    except: break
    for i in range(n,9,-1):
        if temp[i] and temp[i-8:i+1] == quadruplet:
            print(i)
            break

Submission status: Accepted

Solution code:
from math import sqrt, ceil

N = 10100000
temp = [True]*(N+1)
temp[0] = temp[1] = False
for i in range(2, ceil(sqrt(N+1))):
    if temp[i]:
        temp[i+i::i] = [False]*(len(temp[i+i::i]))

quadruplet = [True, False, True, False, False, False, True, False,True]
ans = [0]*N
for i in range(8,N):
    if temp[i] and temp[i-8:i+1] == quadruplet: ans[i] = i
    else: ans[i] = ans[i-1]
while True:
    n = int(input())
    if not n: break
    print(ans[n])

Submission status: Accepted

Solution code:
# from datetime import datetime

# measure_start = datetime.now()
move = [[[10, 10], [4, 4]], [[8, 2],[4, 2]]]

MAX_N = 10000000+1
sos = [1] * (MAX_N+1)
sos[0] = sos[1] = 0
for i in range(2, int(MAX_N**0.5)+1):
    if sos[i]:
        for j in range(i+i, MAX_N+1, i):
            sos[j] = 0

while 1:
    try:
        n = int(input())

        if not n: break
        n -= (n%2 == 0)
        while n >= 13:
            if sos[n] and sos[n-2] and sos[n-6] and sos[n-8]:
                print(n)
                break
            else:
                n -= move[sos[n-8]][sos[n-6]][sos[n-2]]
    # measure_stop = datetime.now()
    # print(measure_stop - measure_start)
    except:
        pass

Submission status: Accepted

Solution code:
def get_quad(n, p):
    for ni in range(n,0,-1):
        if p[ni] and p[ni - 2] and p[ni - 6] and p[ni - 8]:
            return ni
        
def sieve():
    n = 10000001
    p = [1] * n
    p[0] = p[1] = 0
    
    for i in range(int(n ** 0.5)):
        if p[i]:
            for j in range(2 * i, len(p), i):
                p[j] = 0
    return p
import sys
f = sys.stdin

prime = sieve()
while True:
    n = f.readline().strip()
    if not n.isdigit():
        continue
    n = int(n)
    if n == 0:
        break
    print(get_quad(n, prime))

Submission status: Accepted

Solution code:
def get_quad(n, p):
    if n < 19:
        return 13
    n = (n + 1) // 10 * 10 - 1
    for ni in range(n,0,-10):
        if p[ni] and p[ni - 2] and p[ni - 6] and p[ni - 8]:
            return ni
        
def sieve():
    n = 10000001
    p = [1] * n
    p[0] = p[1] = 0
    
    for i in range(int(n ** 0.5)):
        if p[i]:
            for j in range(2 * i, len(p), i):
                p[j] = 0
    return p

import sys
f = sys.stdin

prime = sieve()
while True:
    n = f.readline().strip()
    if not n.isdigit():
        continue
    n = int(n)
    if n == 0:
        break
    print(get_quad(n, prime))

Submission status: Accepted

Solution code:
n=10000000
p=[1]*(n+1)
p[0],p[1]=0,0
for i in range(2,int(n**0.5)+1):
    if p[i]:        
        for j in range(i*i,n+1,i):
            p[j]=0
#p=[i for i in range(n+1) if p[i]==1]
while 1:
    n=int(input())
    if n==0: break
    if n<19:print(13)
    else:
        for i in range((n+1)//10*10-1,2,-10):
            if (p[i],p[i-2],p[i-6],p[i-8])==(1,1,1,1):
                print(i)
                break

Submission status: Accepted

Solution code:
primes = [0, 0] + [1]*9999999
for i in range(2, 3163):
    if primes[i]:
        for j in range(i*i, 10000001, i):
            primes[j] = 0

while True:
    n = int(input())
    if n == 0:
        break
    for i in range(5, n+1)[::-1]:
        if primes[i] and primes[i-2] and primes[i-6] and primes[i-8]:
            print(i)
            break

Submission status: Accepted

Solution code:
# Aizu Problem 0222: Prime Quadruplet

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(10**7)
quad_max = []
for k in range(3, len(primes)):
    p = primes[k]
    if primes[k-3] == p - 8 and primes[k-2] == p - 6 and primes[k-1] == p - 2:
        quad_max.append(p)


while True:
    n = int(input())
    if n == 0:
        break
    idx = bisect.bisect_right(quad_max, n)
    print(quad_max[idx-1])

Submission status: Accepted

Solution code:
primes = [0, 0] + [1]*9999999
for i in range(2, 3163):
    if primes[i]:
        for j in range(i*i, 10000001, i):
            primes[j] = 0
 
while True:
    n = int(input())
    if n == 0:
        break
    for i in range(5, n+1)[::-1]:
        if primes[i] and primes[i-2] and primes[i-6] and primes[i-8]:
            print(i)
            break

Submission status: Accepted

Solution code:
import bisect


primes = [0, 0] + [1] * 9999999
for i in range(2, 3163):
    if primes[i]:
        for j in range(i*i, 10000001, i):
            primes[j] = 0

values = [i for i, v in enumerate(primes) if v]
while True:
    n = int(input())
    if n == 0:
        break
    index = bisect.bisect(values, n)
    for i in range(2, index)[::-1]:
        if all(primes[values[i]-j] for j in [0, 2, 6, 8]):
            print(values[i])
            break

Submission status: Accepted

Solution code:
# AOJ 0222 Prime Quadruplet
# Python3 2018.6.23 bal4u
# From Wekipedia (https://en.wikipedia.org/wiki/Prime_quadruplet)
# All prime quadruplets except {5, 7, 11, 13} are of the form
# {30n + 11, 30n + 13, 30n + 17, 30n + 19} for some integer n.

MAX = 899
diff = (0,3,3,21,22,13,7,39,7,73, \
126,119,88,3,11,66,29,17,53,42, \
101,214,104,298,252,133,255,141,76,91, \
168,81,45,56,203,301,43,66,291,223, \
92,97,442,290,437,281,38,144,549,241, \
29,192,11,518,266,490,122,130,13,329, \
85,209,71,241,290,18,301,52,120,34, \
50,805,276,217,182,53,209,155,77,468, \
448,29,90,85,91,7,146,21,309,234, \
60,213,511,59,273,364,57,77,87,231, \
452,168,154,346,273,588,11,91,322,335, \
140,221,87,442,297,95,321,498,146,182, \
8,38,11,679,111,120,83,36,59,462, \
32,349,448,333,644,63,101,960,161,759, \
255,354,270,52,200,133,112,297,298,27, \
74,577,25,182,280,584,756,266,287,277, \
119,31,561,59,179,630,34,98,1,84, \
217,234,4,48,127,528,679,35,108,15, \
752,60,31,228,559,7,35,56,43,10, \
151,374,297,294,14,60,196,133,18,63, \
63,17,35,290,953,584,66,102,427,4, \
357,507,441,420,802,14,66,171,252,88, \
14,364,32,220,66,256,427,651,52,287, \
987,214,161,319,241,1333,190,325,63,500, \
1026,60,13,112,238,144,137,349,417,32, \
164,196,115,735,200,382,273,104,119,214, \
665,235,297,665,25,34,211,280,542,375, \
188,42,134,573,350,106,17,112,676,1095, \
403,62,193,60,13,116,60,255,609,350, \
7,165,661,25,748,176,10,283,144,987, \
389,59,60,342,112,144,31,98,676,297, \
652,189,56,34,441,50,314,266,29,546, \
297,39,657,46,703,70,270,221,122,767, \
13,134,318,1222,84,650,371,92,164,760, \
318,175,158,679,496,389,273,38,676,270, \
902,228,143,196,18,287,102,409,612,1, \
56,269,311,714,1092,176,34,165,143,438, \
266,249,97,442,105,7,913,81,80,871, \
497,585,574,11,220,94,855,132,473,836, \
301,7,833,63,1145,60,1886,382,111,43, \
111,319,431,108,297,60,878,799,133,472, \
529,420,241,46,231,304,616,1145,595,447, \
589,76,399,865,154,101,119,739,528,673, \
49,994,412,1072,6,25,3,49,126,1079, \
1141,66,220,932,1049,561,692,764,476,248, \
200,1897,658,644,24,399,143,1331,839,1, \
1077,760,11,34,658,36,647,21,528,242, \
98,529,24,1117,192,396,930,224,365,66, \
557,377,757,322,203,335,770,155,97,21, \
665,484,553,321,207,116,574,272,287,253, \
637,259,38,263,62,1268,451,693,756,630, \
357,105,32,581,455,153,540,350,91,210, \
409,270,377,442,490,615,424,52,890,199, \
102,1746,462,749,24,644,540,220,840,1656, \
223,74,434,179,665,923,428,307,875,50, \
2387,276,109,363,529,550,139,798,176,150, \
297,123,66,266,414,17,130,1344,300,1799, \
8,1176,279,351,461,396,112,626,498,931, \
2782,123,1253,780,781,1119,46,39,847,468, \
1037,1144,63,332,294,1082,525,459,220,70, \
231,31,1029,256,290,662,242,98,252,13, \
1008,64,346,1211,119,802,189,272,298,122, \
697,319,195,273,410,1221,365,885,322,52, \
847,165,112,67,812,630,801,87,60,424, \
630,867,231,123,308,396,76,119,60,203, \
17,63,553,931,147,588,127,437,164,43, \
14,371,115,150,354,315,473,3,1221,245, \
36,272,214,24,385,249,182,445,171,35, \
921,300,1558,1250,129,539,476,94,11,227, \
427,151,102,126,2176,71,297,60,413,195, \
190,944,49,554,1102,676,279,78,143,364, \
357,462,1144,1050,218,423,623,364,416,239, \
143,280,248,365,77,77,1529,157,361,514, \
536,31,330,87,193,514,935,227,18,91, \
104,49,133,1149,104,518,396,1015,143,445, \
360,385,680,49,1053,669,647,931,140,231, \
31,1075,483,627,101,1012,714,346,504,60, \
917,1140,1180,98,297,1029,225,1918,406,188, \
368,466,1305,1117,1028,50,150,273,333,101, \
151,146,1100,119,1532,497,487,235,658,322, \
538,210,701,574,136,220,168,105,603,332, \
409,84,81,39,860,309,384,259,266,207, \
872,377,403,248,819,882,931,148,42,371, \
805,325,668,529,409,3151,2016,14,38,1487, \
350,295,266,343,1833,1887,123,370,260,696, \
137,962,410,132,847,1051,426,39,214,546, \
539,493,325,137,112,889,130,237,43,1183, \
118,371,1,580,316,434,2698,53,98,262, \
203,119,31,189,813,119,195,294,1435,1345, \
531,624,210,231,321,553,11,756,1358)

def bsch(x):
	l, r = 0, MAX
	while l < r:
		m = (l + r) >> 1
		if tbl[m] == x: return m
		if tbl[m] < x: l = m+1
		else: r = m
	return l-1;

tbl = [0]*MAX
for i in range(1, MAX): tbl[i] = tbl[i-1] + diff[i]
for i in range(MAX): tbl[i] = tbl[i]*30+19

while 1:
	n = int(input())
	if n == 0: break
	if n < 19: print("13")
	else: print(tbl[bsch(n)])

Submission status: Accepted

Solution code:
# AOJ 0222 Prime Quadruplet
# Python3 2018.6.23 bal4u
# From Wekipedia (https://en.wikipedia.org/wiki/Prime_quadruplet)
# All prime quadruplets except {5, 7, 11, 13} are of the form
# {30n + 11, 30n + 13, 30n + 17, 30n + 19} for some integer n.

MAX = 899
diff = (0,3,3,21,22,13,7,39,7,73, \
126,119,88,3,11,66,29,17,53,42, \
101,214,104,298,252,133,255,141,76,91, \
168,81,45,56,203,301,43,66,291,223, \
92,97,442,290,437,281,38,144,549,241, \
29,192,11,518,266,490,122,130,13,329, \
85,209,71,241,290,18,301,52,120,34, \
50,805,276,217,182,53,209,155,77,468, \
448,29,90,85,91,7,146,21,309,234, \
60,213,511,59,273,364,57,77,87,231, \
452,168,154,346,273,588,11,91,322,335, \
140,221,87,442,297,95,321,498,146,182, \
8,38,11,679,111,120,83,36,59,462, \
32,349,448,333,644,63,101,960,161,759, \
255,354,270,52,200,133,112,297,298,27, \
74,577,25,182,280,584,756,266,287,277, \
119,31,561,59,179,630,34,98,1,84, \
217,234,4,48,127,528,679,35,108,15, \
752,60,31,228,559,7,35,56,43,10, \
151,374,297,294,14,60,196,133,18,63, \
63,17,35,290,953,584,66,102,427,4, \
357,507,441,420,802,14,66,171,252,88, \
14,364,32,220,66,256,427,651,52,287, \
987,214,161,319,241,1333,190,325,63,500, \
1026,60,13,112,238,144,137,349,417,32, \
164,196,115,735,200,382,273,104,119,214, \
665,235,297,665,25,34,211,280,542,375, \
188,42,134,573,350,106,17,112,676,1095, \
403,62,193,60,13,116,60,255,609,350, \
7,165,661,25,748,176,10,283,144,987, \
389,59,60,342,112,144,31,98,676,297, \
652,189,56,34,441,50,314,266,29,546, \
297,39,657,46,703,70,270,221,122,767, \
13,134,318,1222,84,650,371,92,164,760, \
318,175,158,679,496,389,273,38,676,270, \
902,228,143,196,18,287,102,409,612,1, \
56,269,311,714,1092,176,34,165,143,438, \
266,249,97,442,105,7,913,81,80,871, \
497,585,574,11,220,94,855,132,473,836, \
301,7,833,63,1145,60,1886,382,111,43, \
111,319,431,108,297,60,878,799,133,472, \
529,420,241,46,231,304,616,1145,595,447, \
589,76,399,865,154,101,119,739,528,673, \
49,994,412,1072,6,25,3,49,126,1079, \
1141,66,220,932,1049,561,692,764,476,248, \
200,1897,658,644,24,399,143,1331,839,1, \
1077,760,11,34,658,36,647,21,528,242, \
98,529,24,1117,192,396,930,224,365,66, \
557,377,757,322,203,335,770,155,97,21, \
665,484,553,321,207,116,574,272,287,253, \
637,259,38,263,62,1268,451,693,756,630, \
357,105,32,581,455,153,540,350,91,210, \
409,270,377,442,490,615,424,52,890,199, \
102,1746,462,749,24,644,540,220,840,1656, \
223,74,434,179,665,923,428,307,875,50, \
2387,276,109,363,529,550,139,798,176,150, \
297,123,66,266,414,17,130,1344,300,1799, \
8,1176,279,351,461,396,112,626,498,931, \
2782,123,1253,780,781,1119,46,39,847,468, \
1037,1144,63,332,294,1082,525,459,220,70, \
231,31,1029,256,290,662,242,98,252,13, \
1008,64,346,1211,119,802,189,272,298,122, \
697,319,195,273,410,1221,365,885,322,52, \
847,165,112,67,812,630,801,87,60,424, \
630,867,231,123,308,396,76,119,60,203, \
17,63,553,931,147,588,127,437,164,43, \
14,371,115,150,354,315,473,3,1221,245, \
36,272,214,24,385,249,182,445,171,35, \
921,300,1558,1250,129,539,476,94,11,227, \
427,151,102,126,2176,71,297,60,413,195, \
190,944,49,554,1102,676,279,78,143,364, \
357,462,1144,1050,218,423,623,364,416,239, \
143,280,248,365,77,77,1529,157,361,514, \
536,31,330,87,193,514,935,227,18,91, \
104,49,133,1149,104,518,396,1015,143,445, \
360,385,680,49,1053,669,647,931,140,231, \
31,1075,483,627,101,1012,714,346,504,60, \
917,1140,1180,98,297,1029,225,1918,406,188, \
368,466,1305,1117,1028,50,150,273,333,101, \
151,146,1100,119,1532,497,487,235,658,322, \
538,210,701,574,136,220,168,105,603,332, \
409,84,81,39,860,309,384,259,266,207, \
872,377,403,248,819,882,931,148,42,371, \
805,325,668,529,409,3151,2016,14,38,1487, \
350,295,266,343,1833,1887,123,370,260,696, \
137,962,410,132,847,1051,426,39,214,546, \
539,493,325,137,112,889,130,237,43,1183, \
118,371,1,580,316,434,2698,53,98,262, \
203,119,31,189,813,119,195,294,1435,1345, \
531,624,210,231,321,553,11,756,1358)

import bisect

tbl = [0]*MAX
for i in range(1, MAX): tbl[i] = tbl[i-1] + diff[i]
for i in range(MAX): tbl[i] = tbl[i]*30+19
while 1:
	n = int(input())
	if n == 0: break
	if n < 19: print("13")
	else:
		p = bisect.bisect_left(tbl, n)
		if tbl[p] == n: print(n)
		else: print(tbl[p-1])

Submission status: Accepted

Solution code:
r =10000001
p = [1]*r
p[0]= p[1] = 0
for i in range(int(r**0.5)):
	if p[i]:
		p[2*i::i] = [0 for j in range(2*i,r,i)]
		
while 1:
	try:
		n = input()
		if n == 0: break
		n -= 1-n%2
		while any(not p[n-i] for i in [0,2,6,8]):
			n -= 2 + (2 + 4*(1-p[n-6]))*(1-p[n-2])
		print n
	except:
		pass

Submission status: Accepted

Solution code:
ref = [[[10,8],[4,4]],[[10,2],[4,2]]]
r =10000001
p = [1]*r
p[0]= p[1] = 0
for i in range(int(r**0.5)):
	if p[i]:
		p[2*i::i] = [0 for j in range(2*i,r,i)]
		
while 1:
	try:
		n = input()
		if n == 0: break
		n -= 1-n%2
		while any(not p[n-i] for i in [0,2,6,8]):
			n -= ref[p[n-2]][p[n-6]][p[n-8]]
		print n
	except:
		pass

Submission status: Accepted

Solution code:
ref = [[[10,8],[4,4]],[[10,2],[4,2]]]
r =10000001
p = [1]*r
p[0]= p[1] = 0
for i in range(int(r**0.5)):
	if p[i]:
		p[2*i::i] = [0]*len(p[2*i::i])
		
while 1:
	try:
		n = input()
		if n == 0: break
		n -= 1-n%2
		while any(not p[n-i] for i in [0,2,6,8]):
			n -= ref[p[n-2]][p[n-6]][p[n-8]]
		print n
	except:
		pass

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Prime Quadruplet
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0222

"""
import sys

from bisect import bisect_right

limit = 10**7
sieve = [True] * limit
sieve[0] = sieve[1] = False
for i in range(4, limit, 2):
    sieve[i] = False
for i in range(3, int(len(sieve)**0.5)+1, 2):
    if sieve[i]:
        for i in range(i**2, limit, i):
            sieve[i] = False
primes = [i for i, x in enumerate(sieve) if x]
s_primes = set(primes)

quadruplet_primes = [13]
for p in primes:
    if p % 10 == 1 and p+2 in s_primes and p+6 in s_primes and p+8 in s_primes:
        quadruplet_primes.append(p + 8)


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break
        print(quadruplet_primes[bisect_right(quadruplet_primes, n) - 1])


if __name__ == '__main__':
    main(sys.argv[1:])