|
发表于 2016-9-10 23:48:44
|
显示全部楼层
本帖最后由 bacon6581 于 2016-9-11 22:20 编辑
- from time import time
- start=time()
- result=set()
- zhishu=[2,3,5,7]
- zhishu_str=[]
- for i in range(10,1000):
- sq=i**0.5
- if sq==int(sq):
- next
- sq=int(sq)
- pd=True
- for j in zhishu:
- if i/j==int(i/j):
- pd=False
- break
- if j>=sq:
- break
- if pd==True:
- zhishu.append(i)
- for i in range(1001,1000000):
- j=str(i)
- if ('5' not in j) and ('0' not in j) and ('2' not in j) and ('4' not in j) and ('6' not in j) and ('8' not in j):
- sq=i**0.5
- if sq==int(sq):
- next
- sq=int(sq)
- pd=True
- for j in zhishu:
- if i/j==int(i/j):
- pd=False
- break
- if j>=sq:
- break
- if pd==True:
- zhishu_str.append(str(i))
- for i in zhishu:
- if i>1000:
- break
- j=str(i)
- if ('5' not in j) and ('0' not in j) and ('2' not in j) and ('4' not in j) and ('6' not in j) and ('8' not in j):
- zhishu_str.append(str(i))
- zhishu_str.append('5')
- zhishu_str.append('2')
- #print(time()-start)
- while len(zhishu_str)>0:
- if len(zhishu_str[0])<2:
- result.add(zhishu_str[0])
- zhishu_str.remove(zhishu_str[0])
- else:
- k=zhishu_str[0]
- n=1
- zs=True
- while n<len(k):
- n+=1
- k=k[1:]+k[0]
- if k not in zhishu_str:
- zs=False
- zhishu_str.remove(zhishu_str[0])
- break
- if zs==True:
- k=zhishu_str[0]
- result.add(zhishu_str[0])
- zhishu_str.remove(zhishu_str[0])
- n=1
- while n<len(k):
- n+=1
- k=k[1:]+k[0]
- if k in zhishu_str:
- result.add(k)
- zhishu_str.remove(k)
-
- #print(result)
- print(len(result))
- print(time()-start)
复制代码
- >>> ================================ RESTART ================================
- >>>
- {'993319', '373', '193939', '939391', '939193', '1193', '1931', '13', '197', '2', '37199', '19937', '319993', '91193', '31', '71', '9377', '39119', '5', '391939', '11939', '17', '3119', '3', '991', '71993', '93911', '199', '73', '337', '3779', '393919', '719', '11', '919393', '933199', '7937', '311', '331999', '79', '7', '93719', '7793', '999331', '19391', '9311', '99371', '37', '971', '919', '199933', '113', '131', '97', '733'}
- 55
- 7.932184457778931
复制代码
一不小心,杀到了10秒内。介绍下思路:
第一种方法(需半小时):
1、求出1000000以内的所有质数并存放在列表中。
2、判断其中一个数的所有轮转形式是否都在列表中,是的话把这个数及轮转形式都放入集合中。
3、统计集合里元素有多少。
第二种方法(需两分钟):
1、求出1000000以内的所有质数并存放在列表中。
2、如列表中的元素含有0、2、3、4、5、6、8的,直接删掉(2、5)除外。因为他们的轮转中这几个值必然有机会跑到个位,这时就能被2或5整除。(如23轮转到32 , 53轮转到35)
3、再判断新列表中一个数的所有轮转形式是否都在列表中,是的话把这个数及轮转形式都放入集合中。
4、统计集合里元素有多少。
第三种方法(10秒内):
1、求出1000以内的所有质数并存放在列表中(一百万的平方根就是1000)。主要用来判断100万 以内的数是否为质数
2、再求1001-100万之间的一些特殊的质数(如果这个数含有0、2、3、4、5、6、8的,直接Pass掉,用不着判断质数了,原因见方法二的第2点)。
3、剔除10-1000之间其中任意一位含有0、2、3、4、5、6、8的质数。
4、再判断新列表中一个数的所有轮转形式是否都在列表中,是的话把这个数及轮转形式都放入集合中。
5、统计集合里元素有多少。 |
评分
-
查看全部评分
|