Skip to content

Instantly share code, notes, and snippets.

@sheep-snow
Created September 22, 2022 13:40
Show Gist options
  • Select an option

  • Save sheep-snow/0c72767122e85bccff79518e4cdc3032 to your computer and use it in GitHub Desktop.

Select an option

Save sheep-snow/0c72767122e85bccff79518e4cdc3032 to your computer and use it in GitHub Desktop.
import logging
from logging import StreamHandler
logger = logging.getLogger("sample")
_fmtr = logging.Formatter("%(asctime)s %(levelname)s [%(name)s] %(message)s")
_handler = StreamHandler()
_handler.setFormatter(_fmtr)
logger.addHandler(_handler)
logger.setLevel("DEBUG")
def euclidean_algorithm(a: int, b: int, loop_cnt: int = 0) -> int:
"""引数2値の最大公約数をユークリッドの互除法で求める
Usage: EuclideanAlgorithm(896, 176)
Args:
a (int): Value 1
b (int): Value 2
loop_cnt (int, optional): loop counter. Defaults to 0.
Returns:
int: _description_
"""
x, y = a, b
if x > y:
logger.info(f"{loop_cnt+1}:\tx{x} - y{y}, x={x-y}")
x = x - y
elif x < y:
logger.info(f"{loop_cnt+1}:\ty{y} - x{x}, y={y-x}")
y = y - x
else:
logger.info(f"{loop_cnt+1}:\ty{y} == x{x}")
logger.info(f"Found: x{x} == y{y}, LoopCnt: {loop_cnt + 1}")
return x
return euclidean_algorithm(x, y, loop_cnt + 1)
if __name__ == "__main__":
euclidean_algorithm(896, 176)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment