Advertisement
jules0707

euclidian_gcd

Jun 6th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. # uses python3
  2. import sys
  3.  
  4.  
  5. def gcd_naive(n, p):
  6.     current_gcd = 1
  7.     for d in range(2, min(n, p) + 1):
  8.         if n % d == 0 and p % d == 0:
  9.             if d > current_gcd:
  10.                 current_gcd = d
  11.  
  12.     return current_gcd
  13.  
  14.  
  15. def gcd_euclid(n, p):
  16.     gcd = 1
  17.     # if n < 0 or p < 0:
  18.     #     gcd = 0
  19.     # elif n + p == 0:
  20.     #     gcd = 0
  21.  
  22. # From Chromium's style guide: Don't use else after return
  23.     if p == 0:
  24.         gcd = n
  25.         return print(gcd)
  26.     gcd_euclid(p, n % p)
  27.  
  28. # one liner cleaner?
  29. def gcd_one_liner(n, p):
  30.     gcd = n if p == 0 else gcd_one_liner(p, n % p)
  31.     return gcd
  32.  
  33. if __name__ == "__main__":
  34.     user_input = sys.stdin.read()
  35.     a, b = map(int, user_input.split())
  36.     # gcd_euclid(a, b)
  37.     print(gcd_one_liner(a, b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement