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/gcd.py

16 lines
343 B
Python
Raw Normal View History

2023-11-13 12:47:53 +00:00
# Based on https://github.com/AllAlgorithms, python/algorithms/math/common_divisor_count.py
def gcd(a: int, b: int) -> int:
assert a > 0 and b > 0
if a == 1 or b == 1:
return 1
if a == b:
return a
if b > a:
a, b = b, a
while b != 0:
temp = b
b = a % b
a = temp
return a