Codility Python 100 %
这是python中的解决方案,几乎没有解释-
def solution(N):
"""
Problem Statement can be found here-
https://app.codility.com/demo/results/trainingJNNRF6-VG4/
Codility 100%
Idea is count decedent factor in single travers. ie. if 24 is divisible by 4 then it is also divisible by 8
Traverse only up to square root of number ie. in case of 24, 4*4 < 24 but 5*5!<24 so loop through only i*i<N
"""
print(N)
count = 0
i = 1
while i * i <= N:
if N % i == 0:
print()
print("Divisible by " + str(i))
if i * i == N:
count += 1
print("Count increase by one " + str(count))
else:
count += 2
print("Also divisible by " + str(int(N / i)))
print("Count increase by two count " + str(count))
i += 1
return count
运行示例-
if __name__ == '__main__':
# result = solution(24)
# result = solution(35)
result = solution(1)
print("")
print("Solution " + str(result))
""" 示例 1- 24
能被 1 整除 也能被 24 整除 计数增加 2 计数 2
能被 2 整除 也能被 12 整除 计数增加 2 计数 4
能被 3 整除 也能被 8 整除 计数增加 2 计数 6
能被 4 整除 也能被 6 整除 计数增加 2 计数 8
解决方案 8
示例 2- 35
能被 1 整除 也能被 35 整除 计数增加 2 计数 2
能被 5 整除 也能被 7 整除 计数增加 2 计数 4
解决方案 4
示例 3-
1
可被 1 整除 计数加一 1
解决方案 1 """
Github 链接