-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprime_numbers.py
45 lines (39 loc) · 1.17 KB
/
prime_numbers.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
import math
prime_list = [2, 3, 5, 7]
def nextNumber(number=prime_list[-1]):
return max(prime_list[-1], number) + 2
def primeTillN(N):
global prime_list
for prime in prime_list:
if prime > N:
break
# already calculated earlier
yield prime
number = nextNumber()
while number < N:
for prime in prime_list:
if prime > math.sqrt(number):
# No need to check next, no factor available
prime_list.append(number)
yield number
break
elif number % prime == 0:
# Not a prime, factors available
break
else:
# Continue to check the divisiblity with next big prime no from
# already available list
continue
# look for next number
number = nextNumber(number)
if __name__ == '__main__':
import sys
try:
N = int(sys.argv[1])
except IndexError:
N = input("Enter value of N : ")
count = 0
for i in primeTillN(N):
# print i
count += 1
print "Count : %d\nBiggest Prime : %d" % (count, prime_list[-1])