MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / factors_of_a_number

Function factors_of_a_number

maths/factors.py:5–30  ·  view source on GitHub ↗

>>> factors_of_a_number(1) [1] >>> factors_of_a_number(5) [1, 5] >>> factors_of_a_number(24) [1, 2, 3, 4, 6, 8, 12, 24] >>> factors_of_a_number(-24) []

(num: int)

Source from the content-addressed store, hash-verified

3
4
5def 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] = []
17 if num < 1:
18 return facs
19 facs.append(1)
20 if num == 1:
21 return facs
22 facs.append(num)
23 for i in range(2, int(sqrt(num)) + 1):
24 if num % i == 0: # If i is a factor of num
25 facs.append(i)
26 d = num // i # num//i is the other factor of num
27 if d != i: # If d and i are distinct
28 facs.append(d) # we have found another factor
29 facs.sort()
30 return facs
31
32
33if __name__ == "__main__":

Callers

nothing calls this directly

Calls 2

sortMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected