-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_prime.py
More file actions
executable file
·76 lines (52 loc) · 1.74 KB
/
is_prime.py
File metadata and controls
executable file
·76 lines (52 loc) · 1.74 KB
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
70
71
72
73
74
75
76
#!/usr/bin/env python3
import math
from typing import Union
def is_prime(value: Union[int, float]) -> bool:
"""
Test whether a number is a prime number.
This function accepts both integers and floats but the float must
not have a decimal value.
Args:
value: Value to test for primality.
Returns: boolian
"""
# Check the input
if isinstance(value, float):
if not value.is_integer():
raise ValueError('Input value is a non-integer float.')
# Force to int
int_val = int(value)
# Check edge cases
if int_val < 0:
raise ValueError('The primality of negative integers is undefined.')
elif int_val == 0:
raise ValueError('The primality of zero is undefined.')
elif int_val == 1:
return False
elif int_val == 2:
return True
elif int_val == 3:
return True
elif int_val % 2 == 0:
return False
# Determine the maximum denominator to test
max_denominator = min(int_val, math.ceil(math.sqrt(int_val)) + 2)
# Exhaustively tests input with odd numbers
return __prime_checker__(int_val, max_denominator)
def __prime_checker__(test_value: int, max_denominator: int) -> bool:
"""
helper function to test prime numbers
"""
for i in range(3, max_denominator, 2):
if test_value % i == 0:
return False
else:
return True
if __name__ == '__main__':
# Command line usage
import argparse
parser = argparse.ArgumentParser(description='Test primality of a number')
parser.add_argument('number', metavar='integer', type=int,
help='Integer to test for primality')
number = parser.parse_args().number
print(is_prime(number))