@@ -6,7 +6,17 @@ def prime_factors(n: int) -> list:
6
6
"""Find Prime Factors.
7
7
>>> prime_factors(100)
8
8
[2, 2, 5, 5]
9
+ >>> prime_factors(0)
10
+ Traceback (most recent call last):
11
+ ...
12
+ ValueError: Only positive integers have prime factors
13
+ >>> prime_factors(-10)
14
+ Traceback (most recent call last):
15
+ ...
16
+ ValueError: Only positive integers have prime factors
9
17
"""
18
+ if n <= 0 :
19
+ raise ValueError ("Only positive integers have prime factors" )
10
20
pf = []
11
21
while n % 2 == 0 :
12
22
pf .append (2 )
@@ -24,7 +34,17 @@ def number_of_divisors(n: int) -> int:
24
34
"""Calculate Number of Divisors of an Integer.
25
35
>>> number_of_divisors(100)
26
36
9
37
+ >>> number_of_divisors(0)
38
+ Traceback (most recent call last):
39
+ ...
40
+ ValueError: Only positive numbers are accepted
41
+ >>> number_of_divisors(-10)
42
+ Traceback (most recent call last):
43
+ ...
44
+ ValueError: Only positive numbers are accepted
27
45
"""
46
+ if n <= 0 :
47
+ raise ValueError ("Only positive numbers are accepted" )
28
48
div = 1
29
49
temp = 1
30
50
while n % 2 == 0 :
@@ -44,7 +64,17 @@ def sum_of_divisors(n: int) -> int:
44
64
"""Calculate Sum of Divisors.
45
65
>>> sum_of_divisors(100)
46
66
217
67
+ >>> sum_of_divisors(0)
68
+ Traceback (most recent call last):
69
+ ...
70
+ ValueError: Only positive numbers are accepted
71
+ >>> sum_of_divisors(-10)
72
+ Traceback (most recent call last):
73
+ ...
74
+ ValueError: Only positive numbers are accepted
47
75
"""
76
+ if n <= 0 :
77
+ raise ValueError ("Only positive numbers are accepted" )
48
78
s = 1
49
79
temp = 1
50
80
while n % 2 == 0 :
@@ -74,7 +104,6 @@ def euler_phi(n: int) -> int:
74
104
75
105
76
106
if __name__ == "__main__" :
77
- print (prime_factors (100 ))
78
- print (number_of_divisors (100 ))
79
- print (sum_of_divisors (100 ))
80
- print (euler_phi (100 ))
107
+ import doctest
108
+
109
+ doctest .testmod ()
0 commit comments