-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeNumber.py
53 lines (48 loc) · 1.01 KB
/
PrimeNumber.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
51
52
53
# get prime number
import datetime
# version 1
upper_limit = 10000
delta = [0, 0, 0]
counts = [0, 0, 0]
start = datetime.datetime.now()
for _ in range(10):
counts[0] = 0
l1 = []
for x in range(2, upper_limit):
for i in range(2,int(x ** 0.5)+1):
if x % i == 0:
break
else:
#print(x)
counts[0] +=1
l1.append(x)
delta[0] = (datetime.datetime.now() - start).total_seconds()
# version 2
start = datetime.datetime.now()
for _ in range(10):
counts[1] = 1
l2 = [2]
for x in range(3, upper_limit, 2):
for i in range(3, int(x ** 0.5)+1, 2):
if x % i == 0:
break
else:
#print(x)
counts[1] += 1
l2.append(x)
delta[1] = (datetime.datetime.now() - start).total_seconds()
# version 3
start = datetime.datetime.now()
for _ in range(10):
counts[2] = 1
l3 = [2]
for x in range(3,upper_limit,2):
for i in l3:
if x % i == 0:
break
else:
l3.append(x)
counts[2] += 1
delta[2] = (datetime.datetime.now() - start).total_seconds()
print(delta, sep = '\t')
print(counts, sep = '\t')