tdd/math_utils/gcd.py
author Puneeth Chaganti <punchagan@fossee.in>
Mon, 31 Jan 2011 17:19:51 +0530
changeset 154 148520862e53
parent 118 513d43e25927
permissions -rw-r--r--
vcs: Minor fixes to handout. 1. Made madhu's commit shorter to fit in one line. 2. Change Author details to have just the Name. 3. Change some punctuation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
118
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
def gcd(a, b):
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
    """Returns the Greatest Common Divisor of the two integers
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
    passed as arguments.
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
    Args:
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
      a: an integer
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
      b: another integer
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
    Returns: Greatest Common Divisor of a and b
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
    >>> gcd(48, 64)
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
    16
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
    >>> gcd(44, 19)
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
    1
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
    """
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
    if b == 0:
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
        return b
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
    return gcd(b, a%b)
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
if __name__ == "__main__":
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
    import doctest
513d43e25927 Move the code and data files to math_utils directory.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
    doctest.testmod()