Created
December 15, 2010 17:16
-
-
Save bngsudheer/742288 to your computer and use it in GitHub Desktop.
Greatest common divisor using Euclid's algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!usr/bin/python | |
# Author - Sudheer Satyanarayana, http://techchorus.net | |
# Greatest common divisor using Euclid's algorithm | |
# Python 2.6 has Fractions module, see Fractions.gcd(a, b) | |
def gcd(m, n): | |
r = m % n | |
if r == 0: | |
return n | |
else: | |
m = n | |
n = r | |
return gcd(m, n) | |
print "The greatest common divisor of 8 and 12 is %s"%gcd(8, 12) # 4 | |
print "The greatest common divisor of 180 and 48 is %s"%gcd(180, 48) # 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really nice! Because of your code I realize that is not necessary to find out what is the larger and the smaller number