forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountDiv.py
More file actions
24 lines (22 loc) · 863 Bytes
/
countDiv.py
File metadata and controls
24 lines (22 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Codility Problem - 100 percent - N logic was easy but there is a better formula [(largest_divisible) - (smallest_divisible) + 1] within range
def solution(A, B, K):
# Required O(1) complexity
# Within range find the smallest and largest divisible number and use the formula
if A%K == 0 and B%K == 0:
return ((B//K)-(A//K))+1
else:
# Lesson: Had a mistake in the while loop: "A%K == 0" which wont work rather not equal works, for while or if loop review the logic and condition properly
while A%K != 0:
A = A + 1
while B%K != 0:
B = B - 1
#print(A,B)
return ((B//K)-(A//K))+1
"""
# O(N): Correctness 100 percent, Performance 50 percent due to timeouts
count = 0
for i in range(A,B+1):
if i%K == 0:
count += 1
return count
"""