-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo05.py
executable file
·57 lines (44 loc) · 1.25 KB
/
demo05.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
'''
Created on Dec 23, 2014
@author: cleonard
'''
from reportlab.lib.validators import isInt
def is_prime(num):
retval = True
bad_input = False
# check that num is integer >= 0
if isInt(num):
num = int(num)
else:
bad_input = True
if not bad_input:
if num < 0:
bad_input = True
if bad_input:
e = Exception("Bad argument detected. " +
"Please pass in a non-negative integer.")
raise(e)
# =======================================================================
# if num <= 3:
# return(num >= 2)
# =======================================================================
if num in (0, 1):
retval = False
elif num == 2:
retval = True
elif num % 2 == 0:
retval = False
else:
for factor in range(3, int(num ** 0.5) + 1, 2):
if num % factor == 0:
retval = False
break
return(retval)
global _debug
_debug = True
if __name__ == '__main__':
print("Gimme a number*, bub.")
print("")
prime_candidate = raw_input("* but it had better be a non-negative integer! ")
p = is_prime(prime_candidate)
print("{0} is prime: {1}".format(prime_candidate, p))