Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active March 2, 2024 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdkfz181tiger/eb575dcf5ce1fa30ceb0e82dcd267e3a to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/eb575dcf5ce1fa30ceb0e82dcd267e3a to your computer and use it in GitHub Desktop.
ユークリッドの互除法
# coding: utf-8
"""
ユークリッドの互除法
"""
#==========
# Main
import math
def main():
print("main!!")
n1 = 140
n2 = 56
print("gcd_odd:{0}".format(gcd_odd(n1, n2)))
print("gcd_diff:{0}".format(gcd_diff(n1, n2)))
# 余りを利用して計算する
def gcd_odd(a, b):
higher = max(a, b)
lower = min(a, b)
remain = higher % lower
if remain == 0: return lower
return gcd_odd(lower, remain)
# 引き算を利用して計算する
def gcd_diff(a, b):
higher = max(a, b)
lower = min(a, b)
diff = higher - lower
if diff == 0: return lower
return gcd_diff(lower, diff)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment