Created
April 16, 2014 03:22
-
-
Save fengs/10802718 to your computer and use it in GitHub Desktop.
From http://stackoverflow.com/questions/11175131/code-for-greatest-common-divisor-in-python
python's built in gcd function
This file contains hidden or 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
>>> from fractions import gcd | |
>>> gcd(20,8) | |
4 | |
>>> print inspect.getsource(gcd) | |
def gcd(a, b): | |
"""Calculate the Greatest Common Divisor of a and b. | |
Unless b==0, the result will have the same sign as b (so that when | |
b is divided by it, the result comes out positive). | |
""" | |
while b: | |
a, b = b, a%b | |
return a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment