This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
kse-02/benchmark/common_divisor_count.py
github-classroom[bot] 24600885b9
Initial commit
2023-11-13 12:47:53 +00:00

27 lines
631 B
Python

# Based on https://github.com/AllAlgorithms, python/algorithms/math/common_divisor_count.py
"""
The function takes two integers as input and return the number of common divisors of
that pair
"""
def cd_count(a: int, b: int) -> int:
if a == 0 or b == 0:
return 2
a = (-1 * a if a < 0 else a)
b = (-1 * b if b < 0 else b)
result = 0
while a != 0:
c = a
a = b % a
b = c
for i in range(1, int((b ** 0.5) + 1)):
if b % i == 0:
if int(b / i) == i:
result = result + 1
else:
result = result + 2
return result