tdd/math_utils/gcd.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 07 Sep 2010 16:56:09 +0530
changeset 130 e5d183dce985
parent 118 513d43e25927
permissions -rw-r--r--
RBT Levels to the questions.
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()