-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest06.py
More file actions
34 lines (32 loc) · 993 Bytes
/
Copy pathtest06.py
File metadata and controls
34 lines (32 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# coding=utf-8
'''
筛素数
题目:指定最大范围 n,判断 2-n 之间有多少个素数,并输出所有素数(二分搜索筛选素数)
'''
import math
# ------ 二分搜索 ------
def binarySearch(numList ,destValue):
low = 0
high = len(numList)-1
while low<=high:
mid = (low + high) / 2
if numList[mid] == destValue: return mid
elif numList[mid] < destValue: low = mid + 1
else: high = mid - 1
return -mid-1
# ------ 筛选素数 ------
def getMaxRangePrimeNumList(maxNum):
result = []
src = range(2, maxNum+1)
while len(src)>0:
a = src.pop(0)
for i in src:
if i%a==0:src.remove(i)
result.append(a)
return result
def getRangePrimeNumList(minNum, maxNum):
result=getMaxRangePrimeNumList(maxNum)
index = abs(binarySearch(result, minNum))
return result[index:]
result = getRangePrimeNumList(100, 200)
print "共有%d 个素数,素数列表为:" % len(result),result