forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_of_divisors.py
51 lines (37 loc) · 1005 Bytes
/
sum_of_divisors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Python program to get sum of divisors.
The Sum of divisors refers to the sum of numbers in the range [1 to n] which perfectly divides n.
"""
import math
# Function to get the sum of divisors
def sum_of_divisors(num):
# Initializing i as 1 and sum as 0
i = 1
sum = 0
while True:
if i > math.sqrt(num):
break
# Adding i to sum if it is a factor
if num % i == 0 and i == math.sqrt(num):
sum += i
# Adding i and n/i to sum if i is a factor
elif num % i == 0:
sum += i
sum += num // i
i += 1
return sum
if __name__ == '__main__':
# Taking Input
n = int(input("Enter the number :"))
# Printing Output
print("The sum of the divisors is: {} ".format(sum_of_divisors(n)))
"""
Time Complexity - O(sqrt(n)), where 'n' is the number
Space Complexity - O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE I
INPUT
Enter the number: 36
OUTPUT
The sum of the divisors is: 91
"""