#! /usr/bin/env python
from math import *
def isprime(n):
for i in [2] + [x for x in range(3, int(sqrt(n)) + 1) if x % 2 != 0]:
if n % i == 0:
return False
return True
def genprimes(n):
result = []
for i in range(2, n):
if isprime(i):
result.append(i)
return result
def answer(n, primes, pos, r):
if r - 1 <= 1:
return primes[pos]
kk = n + primes[pos]
if sqrt(kk) == ceil(sqrt(kk)):
print "[{:2}] {}".format(21 - r, primes[pos])
return answer(primes[pos], primes, pos + 1, r - 1)
return answer(n, primes, pos + 1, r)
print "[ 0] 2"
print answer(2, genprimes(20000), 0, 20)
IyEgL3Vzci9iaW4vZW52IHB5dGhvbgoKZnJvbSBtYXRoIGltcG9ydCAqCgpkZWYgaXNwcmltZShuKToKICAgIGZvciBpIGluIFsyXSArIFt4IGZvciB4IGluIHJhbmdlKDMsIGludChzcXJ0KG4pKSArIDEpIGlmIHggJSAyICE9IDBdOgogICAgICAgIGlmIG4gJSBpID09IDA6CiAgICAgICAgICAgIHJldHVybiBGYWxzZQoKICAgIHJldHVybiBUcnVlCgpkZWYgZ2VucHJpbWVzKG4pOgogICAgcmVzdWx0ID0gW10KICAgIGZvciBpIGluIHJhbmdlKDIsIG4pOgogICAgICAgIGlmIGlzcHJpbWUoaSk6CiAgICAgICAgICAgIHJlc3VsdC5hcHBlbmQoaSkKCiAgICByZXR1cm4gcmVzdWx0CgpkZWYgYW5zd2VyKG4sIHByaW1lcywgcG9zLCByKToKICAgIGlmIHIgLSAxIDw9IDE6CiAgICAgICAgcmV0dXJuIHByaW1lc1twb3NdCgogICAga2sgPSBuICsgcHJpbWVzW3Bvc10KICAgIGlmIHNxcnQoa2spID09IGNlaWwoc3FydChraykpOgogICAgICAgIHByaW50ICJbezoyfV0ge30iLmZvcm1hdCgyMSAtIHIsIHByaW1lc1twb3NdKQogICAgICAgIHJldHVybiBhbnN3ZXIocHJpbWVzW3Bvc10sIHByaW1lcywgcG9zICsgMSwgciAtIDEpCgogICAgcmV0dXJuIGFuc3dlcihuLCBwcmltZXMsIHBvcyArIDEsIHIpCgoKcHJpbnQgIlsgMF0gMiIKcHJpbnQgYW5zd2VyKDIsIGdlbnByaW1lcygyMDAwMCksIDAsIDIwKQ==