forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample3.py
More file actions
33 lines (24 loc) · 731 Bytes
/
example3.py
File metadata and controls
33 lines (24 loc) · 731 Bytes
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
# ch9/example1.py
from math import sqrt
from timeit import default_timer as timer
def is_prime(x):
print('Processing %i...' % x)
if x < 2:
print('%i is not a prime number.' % x)
elif x == 2:
print('%i is a prime number.' % x)
elif x % 2 == 0:
print('%i is not a prime number.' % x)
else:
limit = int(sqrt(x)) + 1
for i in range(3, limit, 2):
if x % i == 0:
print('%i is not a prime number.' % x)
return
print('%i is a prime number.' % x)
if __name__ == '__main__':
start = timer()
is_prime(9637529763296797)
is_prime(427920331)
is_prime(157)
print('Took %.2f seconds.' % (timer() - start))