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

Function number_of_divisors

maths/basic_maths.py:34–63  ·  view source on GitHub ↗

Calculate Number of Divisors of an Integer. >>> number_of_divisors(100) 9 >>> number_of_divisors(0) Traceback (most recent call last): ... ValueError: Only positive numbers are accepted >>> number_of_divisors(-10) Traceback (most recent call last):

(n: int)

Source from the content-addressed store, hash-verified

32
33
34def number_of_divisors(n: int) -> int:
35 """Calculate Number of Divisors of an Integer.
36 >>> number_of_divisors(100)
37 9
38 >>> number_of_divisors(0)
39 Traceback (most recent call last):
40 ...
41 ValueError: Only positive numbers are accepted
42 >>> number_of_divisors(-10)
43 Traceback (most recent call last):
44 ...
45 ValueError: Only positive numbers are accepted
46 """
47 if n <= 0:
48 raise ValueError("Only positive numbers are accepted")
49 div = 1
50 temp = 1
51 while n % 2 == 0:
52 temp += 1
53 n = int(n / 2)
54 div *= temp
55 for i in range(3, int(math.sqrt(n)) + 1, 2):
56 temp = 1
57 while n % i == 0:
58 temp += 1
59 n = int(n / i)
60 div *= temp
61 if n > 1:
62 div *= 2
63 return div
64
65
66def sum_of_divisors(n: int) -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected