Mapytex/pymath/arithmetic.py

43 lines
869 B
Python
Raw Normal View History

#!/usr/bin/env python
# encoding: utf-8
def gcd(a, b):
2013-08-07 23:12:11 +00:00
"""Compute gcd(a,b)
2013-08-07 23:12:11 +00:00
:param a: first number
:param b: second number
:returns: the gcd
2013-08-07 23:12:11 +00:00
"""
pos_a, _a = (a >= 0), abs(a)
pos_b, _b = (b >= 0), abs(b)
gcd_sgn = (-1 + 2*(pos_a or pos_b))
if _a > _b:
c = _a % _b
2013-08-07 23:12:11 +00:00
else:
c = _b % _a
2013-08-07 23:12:11 +00:00
if c == 0:
return gcd_sgn * min(_a,_b)
elif _a == 1:
return gcd_sgn * _b
elif _b == 1:
return gcd_sgn * _a
2013-08-07 23:12:11 +00:00
else:
return gcd_sgn * gcd(min(_a,_b), c)
if __name__ == '__main__':
print(gcd(3, 15))
print(gcd(3, 15))
print(gcd(-15, -3))
print(gcd(-3, -12))
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del