-
Notifications
You must be signed in to change notification settings - Fork 54
/
素数.py
51 lines (43 loc) · 1.24 KB
/
素数.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#方法一:
#这效率比较低,追求效率的话可以试试下一个方法
def prime_number(num):
res = True
if num <= 1:
res = False
return res
for x in range(2,num-1):
if num%x==0:
res = False
return res
return res
print([x for x in range(100) if prime_number(x)])
#方法二:
#如果要追求一下速度的话,可以试试这个(求1千万内素数14秒左右秒).
def getPrime(maxNum):
aList = [x for x in range(0, maxNum)]
prime = []
for i in range(2, len(aList)):
if aList[i] != 0:
prime.append(aList[i])
clear(aList[i], aList, maxNum)
return prime
def clear(aPrime, aList, maxNum):
for i in range(2, int((maxNum / aPrime) + 1)):
if not aPrime * i > maxNum - 1:
aList[i * aPrime] = 0
print(getPrime(100))
#方法三
#这个和方法二差不多,只是把两个函数合并在一块了
def fun(max):
l = []
a = list(range(max + 1))
for i in range(2, max):
if a[i]:
l.append(a[i])
for j in range(2, int(max/a[i])+1):
if j * a[i] <= max - 1:
a[a[i] * j] = 0
print(l)
fun(10000000)