-
Notifications
You must be signed in to change notification settings - Fork 1k
/
count_of_divisors.py
50 lines (36 loc) · 961 Bytes
/
count_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
"""
Python program to get count of divisors.
Count of divisors refers to the count of numbers in the range [1 to n] which perfectly divides n.
"""
import math
# Function to calculate the count of divisors
def count_of_divisors(num):
i = 1
# Initializing cnt by 0
cnt = 0
while True:
if i > math.sqrt(num):
break
# Increasing cnt by 1 for i
if num % i == 0 and i == math.sqrt(num):
cnt += 1
# Increasing cnt by 2 for i and n/i
elif num % i == 0:
cnt += 2
i += 1
return cnt
if __name__ == '__main__':
# Taking Input
n = int(input('Enter the number: '))
# Printing Output
print("The count of the divisors is: {} ".format(count_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 count of the divisors is: 9
"""