展开全部
#!/usr/bin/python
# encoding: utf-8
import time
import pprint
def allPrime(maxNum):
aList = range(0, maxNum)
def _do(aPrime):
for i in xrange(aPrime, maxNum, aPrime):
aList[i] = 0
return aPrime
return map(_do,
(i for i in xrange(2,maxNum) if aList[i])
)
t0 = time.time()
LIMIT = 1000000
primelist = allPrime(LIMIT)
primesets = set(primelist)
def loopshift(num):
numlst = list(str(num))
result = list()
for i in range(len(numlst)):
x = int(''.join(numlst[i:] + numlst[:i]))
if x not in primesets:
return None
result.append(x)
result = sorted(set(result))
for x in set(result):
primesets.remove(x)
return result
found = list()
for num in primelist:
xn = loopshift(num)
if xn and xn not in found:
found.append(xn)
pprint.pprint(found)
print "Process usage", time.time()-t0
>python -u "baidu.py"
[[2],
[3],
[5],
[7],
[11],
[13, 31],
[17, 71],
[37, 73],
[79, 97],
[113, 131, 311],
[197, 719, 971],
[199, 919, 991],
[337, 373, 733],
[1193, 1931, 3119, 9311],
[3779, 7793, 7937, 9377],
[11939, 19391, 39119, 91193, 93911],
[19937, 37199, 71993, 93719, 99371],
[193939, 391939, 393919, 919393, 939193, 939391],
[199933, 319993, 331999, 933199, 993319, 999331]]
Process usage 0.953225135803
>Exit code: 0 Time: 1.033