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

Function sum_of_divisors

maths/basic_maths.py:66–95  ·  view source on GitHub ↗

Calculate Sum of Divisors. >>> sum_of_divisors(100) 217 >>> sum_of_divisors(0) Traceback (most recent call last): ... ValueError: Only positive numbers are accepted >>> sum_of_divisors(-10) Traceback (most recent call last): ... ValueError: O

(n: int)

Source from the content-addressed store, hash-verified

64
65
66def sum_of_divisors(n: int) -> int:
67 """Calculate Sum of Divisors.
68 >>> sum_of_divisors(100)
69 217
70 >>> sum_of_divisors(0)
71 Traceback (most recent call last):
72 ...
73 ValueError: Only positive numbers are accepted
74 >>> sum_of_divisors(-10)
75 Traceback (most recent call last):
76 ...
77 ValueError: Only positive numbers are accepted
78 """
79 if n <= 0:
80 raise ValueError("Only positive numbers are accepted")
81 s = 1
82 temp = 1
83 while n % 2 == 0:
84 temp += 1
85 n = int(n / 2)
86 if temp > 1:
87 s *= (2**temp - 1) / (2 - 1)
88 for i in range(3, int(math.sqrt(n)) + 1, 2):
89 temp = 1
90 while n % i == 0:
91 temp += 1
92 n = int(n / i)
93 if temp > 1:
94 s *= (i**temp - 1) / (i - 1)
95 return int(s)
96
97
98def euler_phi(n: int) -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected