fork download
  1. #! /usr/bin/env python
  2.  
  3. from math import *
  4.  
  5. def isprime(n):
  6. for i in [2] + [x for x in range(3, int(sqrt(n)) + 1) if x % 2 != 0]:
  7. if n % i == 0:
  8. return False
  9.  
  10. return True
  11.  
  12. def genprimes(n):
  13. result = []
  14. for i in range(2, n):
  15. if isprime(i):
  16. result.append(i)
  17.  
  18. return result
  19.  
  20. def answer(n, primes, pos, r):
  21. if r - 1 <= 1:
  22. return primes[pos]
  23.  
  24. kk = n + primes[pos]
  25. if sqrt(kk) == ceil(sqrt(kk)):
  26. print "[{:2}] {}".format(21 - r, primes[pos])
  27. return answer(primes[pos], primes, pos + 1, r - 1)
  28.  
  29. return answer(n, primes, pos + 1, r)
  30.  
  31.  
  32. print "[ 0] 2"
  33. print answer(2, genprimes(20000), 0, 20)
Runtime error #stdin #stdout 0.44s 11464KB
stdin
Standard input is empty
stdout
[ 0] 2
[ 1] 7
[ 2] 29
[ 3] 71
[ 4] 73
[ 5] 251
[ 6] 773
[ 7] 827
[ 8] 937
[ 9] 1367
[10] 1549
[11] 5507
[12] 7489