-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes.py
69 lines (61 loc) · 1.31 KB
/
primes.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import math
def isprime(num):
if num == 1: return False
if num == 2: return True
if not num % 2: return False
divisor = 3
sqrt_divided = int(math.sqrt(num))
for i in xrange(3, sqrt_divided + 1, 2):
if not num % i:
return False
return True
def prime_list2(max_number):
grid = prime_list(max_number)
primes = []
for i in xrange(0, len(grid)) :
if grid[i]:
primes.append(i)
return primes
def prime_list( max_number):
# Find all the primes up to max_number
grid = [True] * (max_number + 1) #set array of all numbers
grid[0] = False
grid[1] = False
for i in xrange(2, max_number + 1, 2):
grid[i] = False
grid[2] = True
for num in xrange(3, max_number + 1, 2):
try:
if grid[num] == True: #if we hit a new prime
for i in xrange(num, max_number, num):
if i != num:
grid[i] = False #mark multiples of the prime as not-prime
except:
pass
return grid
def next_prime(num):
#find the next prime given num
prime = False
while not prime:
num += 1
prime = isprime(num)
return num
'''
import time
start = time.time()
print prime_list(200)[0]
print time.time() - start
start = time.time()
print next_prime(2000000000003), time.time() - start
print isprime(8)
print isprime(2000000000048)
'''
'''
x = prime_list(200)
for i in (0, 200):
try:
if x[i]:
print i
except:
pass
'''