forked from joaks1/python-script-best-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallest_factor.py
More file actions
executable file
·48 lines (39 loc) · 1.13 KB
/
Copy pathsmallest_factor.py
File metadata and controls
executable file
·48 lines (39 loc) · 1.13 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
#! /usr/bin/env python3
"A modules for getting the smallest prime factors of an integer."
import sys
def get_smallest_prime_factor(n):
"""
Returns the smallest integer that is a factor of `n`.
If `n` is a prime number, `None` is returned.
Parameters
----------
n : int
The integer to be factored.
Returns
-------
int or None
The smallest integer that is a factor of `n`
or Nono if `n` is a prime.
Examples
--------
>>> get_smallest_prime_factor(7)
>>> get_smallest_prime_factor(8)
2
>>> get_smallest_prime_factor (9)
3
"""
for i in range(2, n):
if (n % i) == 0:
return i
return None
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit(sys.argv[0] + ": Expecting one command line argument -- the integer for which to get the smallest factor")
n = int(sys.argv[1])
if n < 1:
sys.exit(sys.argv[0] + ": Expecting a positive integer")
smallest_prime_factor = get_smallest_prime_factor(n)
if smallest_prime_factor is None:
print(n)
else:
print(smallest_prime_factor)