Skip to content

Commit 7aa086f

Browse files
Update factors.py with minor fixes
removing readability by adding docstring, comment and improve the edge case check
1 parent 68473af commit 7aa086f

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

maths/factors.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
from doctest import testmod
22
from math import sqrt
33

4-
5-
def factors_of_a_number(num: int) -> list:
6-
"""
7-
>>> factors_of_a_number(1)
8-
[1]
9-
>>> factors_of_a_number(5)
10-
[1, 5]
11-
>>> factors_of_a_number(24)
12-
[1, 2, 3, 4, 6, 8, 12, 24]
13-
>>> factors_of_a_number(-24)
14-
[]
15-
"""
16-
facs: list[int] = []
4+
def factors_of_a_number(num: int) -> list[int]:
5+
"""Return all factors of a positive integer in sorted order."""
6+
177
if num < 1:
18-
return facs
8+
raise ValueError("num must be a positive integer")
9+
facs: list[int] = []
1910
facs.append(1)
2011
if num == 1:
2112
return facs
22-
facs.append(num)
13+
facs.append(num) #num is always a factor of itself
2314
for i in range(2, int(sqrt(num)) + 1):
2415
if num % i == 0: # If i is a factor of num
2516
facs.append(i)

0 commit comments

Comments
 (0)