lolamontes69

Python/ Greatest Common Divisor using Euclid's Algorithm

May 19th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. def find_gcd(a, b):
  2.     if a > b:
  3.         c = int(a - b)
  4.         if c == 0: print "a =",a,"and b =",b
  5.         elif c == 1:
  6.             print "GCD = 1"
  7.             exit
  8.         else: return find_gcd(c, b)
  9.     elif b > a:
  10.         c = int(b - a)
  11.         if c == 0: print "a =",a,"and b =",b
  12.         elif c == 1:
  13.             print "GCD = 1"
  14.             exit
  15.         else: return find_gcd(a, c)
  16.     else:
  17.         print "GCD =",a
  18.         exit
  19.  
  20. a = int(raw_input('Value of a\n'))
  21. b = int(raw_input('Value of b\n'))
  22. find_gcd(a, b)
  23.  
  24.  
  25. """ In its simplest form, Euclid's algorithm starts with a pair of positive integers and forms a new pair that consists of the smaller number and the difference between the larger and smaller numbers.
  26. The process repeats until the numbers are equal.
  27. That number then is the greatest common divisor of the original pair.
  28. Note: Lowest common divisor is 1
  29. """
Advertisement
Add Comment
Please, Sign In to add comment