-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrime Number.py
47 lines (33 loc) · 925 Bytes
/
Prime Number.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
import time, math
import textwrap
def is_prime(n):
'''Return "True" if "n" is a prime number. False otherwise. '''
if n ==1:
return False # 1 is not a prime number
#If it's even and not 2, then it's not prime
if n ==2:
return True
if n > 2 and n%2==0:
return False
max_divisor = math.floor(math.sqrt(n))
for d in range(3, max_divisor,2):
if n%d == 0:
return False
break
return True
#========Test Function ===========
t0 = time.time()
prime_list = []
upper = 100_000;
for n in range(1, upper):
prime = is_prime(n)
if prime == True:
prime_list.append(n)
t1 = time.time()
print("Time required: ", t1 -t0)
#convert a list of integer to string
prime_list = ''.join(str(prime_list))
wrapper = textwrap.TextWrapper(width=150)
word_list = wrapper.wrap(text= prime_list)
for element in word_list:
print(element)