Hướng dẫn how do you check if a fraction can be reduced python? - Làm thế nào để bạn kiểm tra xem một phân số có thể được giảm bớt python không?

Mô -đun fractions có thể làm điều đó

Show
>>> from fractions import Fraction
>>> Fraction(98, 42)
Fraction(7, 3)

Có một công thức ở đây cho một gcd numpy. Mà sau đó bạn có thể sử dụng để phân chia phân số của bạn

>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)

#find greatest common divisor for numerator and denominator, except 1: common_factor = 1 for i in xrange(min(abs(numer), abs(denom)), 1, -1): if numer % i == 0 and denom % i == 0: common_factor = i break 35 fractions6#find greatest common divisor for numerator and denominator, except 1: common_factor = 1 for i in xrange(min(abs(numer), abs(denom)), 1, -1): if numer % i == 0 and denom % i == 0: common_factor = i break 80#find greatest common divisor for numerator and denominator, except 1: common_factor = 1 for i in xrange(min(abs(numer), abs(denom)), 1, -1): if numer % i == 0 and denom % i == 0: common_factor = i break 38#find greatest common divisor for numerator and denominator, except 1: common_factor = 1 for i in xrange(min(abs(numer), abs(denom)), 1, -1): if numer % i == 0 and denom % i == 0: common_factor = i break 8282

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
60/03
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
49
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
82
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
89

0/07A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python. def reducefract(n, d): '''Reduces fractions. n is the numerator and d the denominator.''' def gcd(n, d): while d != 0: t = d d = n%d n = t return n assert d!=0, "integer division by zero" assert isinstance(d, int), "must be int" assert isinstance(n, int), "must be int" greatest=gcd(n,d) n/=greatest d/=greatest return n, d 4 #find greatest common divisor for numerator and denominator, except 1: common_factor = 1 for i in xrange(min(abs(numer), abs(denom)), 1, -1): if numer % i == 0 and denom % i == 0: common_factor = i break 80>>> def numpy_gcd(a, b): ... a, b = np.broadcast_arrays(a, b) ... a = a.copy() ... b = b.copy() ... pos = np.nonzero(b)[0] ... while len(pos) > 0: ... b2 = b[pos] ... a[pos], b[pos] = b2, a[pos] % b2 ... pos = pos[b[pos]!=0] ... return a ... >>> numpy_gcd(np.array([98]), np.array([42])) array([14]) >>> 98/14, 42/14 (7, 3) 13

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
6
A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.


def reducefract(n, d):
    '''Reduces fractions. n is the numerator and d the denominator.'''
    def gcd(n, d):
        while d != 0:
            t = d
            d = n%d
            n = t
        return n
    assert d!=0, "integer division by zero"
    assert isinstance(d, int), "must be int"
    assert isinstance(n, int), "must be int"
    greatest=gcd(n,d)
    n/=greatest
    d/=greatest
    return n, d
4 fractions6
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
82
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
38
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
80
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
00
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
82 ____251

  1. #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    37
    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
    
    05
  2. #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    39
    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
    
    07
  3. JavaScript
  4. 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
    
    14

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
35
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
16

#find greatest common divisor for numerator and denominator, except 1: common_factor = 1 for i in xrange(min(abs(numer), abs(denom)), 1, -1): if numer % i == 0 and denom % i == 0: common_factor = i break 6def 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 27def 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 7 fractions0def 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 9 fractions2

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
35
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
34

  • x = 8, y = 5
  • Độ phức tạp về thời gian: O (log (max (x, y))))
  • Không gian phụ trợ: O (log (max (x, y))) & nbsp;
  • Làm thế nào để bạn biết nếu một phân số có thể được giảm?

Một phân số được coi là đơn giản hóa nếu không có yếu tố chung, ngoài 1, trong tử số và mẫu số. Nếu một phân số có các yếu tố phổ biến trong tử số và mẫu số, chúng ta có thể giảm phân số xuống dạng đơn giản hóa của nó bằng cách loại bỏ các yếu tố phổ biến.

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break

Làm thế nào để bạn kiểm tra phân số trong Python?

Phân số (tử số = 0, mẫu số = 1): Điều này yêu cầu tử số và mẫu số là các trường hợp của số. Rational và một thể hiện phân số với value = (tử số/mẫu số) được trả về. Một lỗi Zerodivision được nêu ra nếu mẫu số = 0.

Bạn có thể sử dụng phân số trong Python không?

Để tất cả chúng cùng nhau:

Có lẽ có nhiều triển khai Python của thuật toán Euclide, tôi đã sao chép một từ https://stackoverflow.com/a/11175154/1187415:

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

Như bạn có thể thấy, việc chia mẫu số cho ước số chung cho kết quả tích cực, điều đó có nghĩa là bước 3 không cần thiết nữa. Điều này cũng có nghĩa là phân số đã được đơn giản hóa chính xác nếu ước số chung của tử số và mẫu số bằng một.

Vì vậy, phương pháp chính của bạn giảm xuống

def simplify_fraction(numer, denom):

    if denom == 0:
        return "Division by 0 - result undefined"

    # Remove greatest common divisor:
    common_divisor = gcd(numer, denom)
    (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
    # Note that reduced_den > 0 as documented in the gcd function.

    if reduced_den == 1:
        return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
    elif common_divisor == 1:
        return "%d/%d is already at its most simplified state" % (numer, denom)
    else:
        return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)

A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.


def reducefract(n, d):
    '''Reduces fractions. n is the numerator and d the denominator.'''
    def gcd(n, d):
        while d != 0:
            t = d
            d = n%d
            n = t
        return n
    assert d!=0, "integer division by zero"
    assert isinstance(d, int), "must be int"
    assert isinstance(n, int), "must be int"
    greatest=gcd(n,d)
    n/=greatest
    d/=greatest
    return n, d

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
35 fractions6
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
80
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
38
#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
8282

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
6
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
34
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
31
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
12
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
13

#find greatest common divisor for numerator and denominator, except 1:
common_factor = 1
for i in xrange(min(abs(numer), abs(denom)), 1, -1):
    if numer % i == 0 and denom % i == 0:
         common_factor = i
         break
6
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
41
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
31
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
17
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
13

>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
6
>>> def numpy_gcd(a, b):
...     a, b = np.broadcast_arrays(a, b)
...     a = a.copy()
...     b = b.copy()
...     pos = np.nonzero(b)[0]
...     while len(pos) > 0:
...         b2 = b[pos]
...         a[pos], b[pos] = b2, a[pos] % b2
...         pos = pos[b[pos]!=0]
...     return a
... 
>>> numpy_gcd(np.array([98]), np.array([42]))
array([14])
>>> 98/14, 42/14
(7, 3)
73

  • A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    7
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    8
  • #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    95
    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
    
    7 fractions0
    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
    
    9 fractions2
  • #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    34
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    31
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    12
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    13

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    41
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    31
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    17
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    13

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    73

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    7
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    8x and y and where x is divisible by y. It can be represented in the form of a fraction x/y. The task is to reduce the fraction to its lowest form.
    Examples: 
     

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    95
    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
    
    7 fractions0
    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
    
    9 fractions2
    Both of the values x and y will be divisible by their greatest common divisor. So if we divide x and y from the gcd(x, y) then x and y can be reduced to its simplest form.
    Below is the implementation of the above approach: 
     

    C++

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    5

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    7
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    8

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    2
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    8

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    2

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    6
    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
    
    7
    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
    
    8
    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
    
    9
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    0

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    3

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    7

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    2

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    5

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    Java

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    7
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    8

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    2
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    8

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    2

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    6
    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
    
    7
    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
    
    8
    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
    
    9
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    0

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    3

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    7

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    5

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    Java

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    7
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    8

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    2
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    2

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    Python3

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    x = 8, y = 5
    8
    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
    
    7 fractions0
    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
    
    9 fractions2

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1 fractions6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1 fractions8
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1 0/00

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    60/03 0/04
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    00/06

    0/07

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4 0/09

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    02

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    48
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    49
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    50
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    51
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    52
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    53

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    04
    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    07

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    11
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    122

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    16
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    17
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    13

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    2

    C#

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    23
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    24
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    25
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    26

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    7
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    8

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    2
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    8

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    2

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    x = 8, y = 5
    8
    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
    
    7 fractions0
    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
    
    9 fractions2

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    3

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    7

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    5

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    Java

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    7

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    2

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1: common_factor = 1 for i in xrange(min(abs(numer), abs(denom)), 1, -1): if numer % i == 0 and denom % i == 0: common_factor = i break 6A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python. def reducefract(n, d): '''Reduces fractions. n is the numerator and d the denominator.''' def gcd(n, d): while d != 0: t = d d = n%d n = t return n assert d!=0, "integer division by zero" assert isinstance(d, int), "must be int" assert isinstance(n, int), "must be int" greatest=gcd(n,d) n/=greatest d/=greatest return n, d 4 A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python. def reducefract(n, d): '''Reduces fractions. n is the numerator and d the denominator.''' def gcd(n, d): while d != 0: t = d d = n%d n = t return n assert d!=0, "integer division by zero" assert isinstance(d, int), "must be int" assert isinstance(n, int), "must be int" greatest=gcd(n,d) n/=greatest d/=greatest return n, d 5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    34

    Java

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    43
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    13

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    7
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    8

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    2
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    4

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    x = 8, y = 5
    8
    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
    
    7 fractions0
    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
    
    9 fractions2

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1 fractions6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1 fractions8
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1 0/00

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    60/03 0/04
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    00/06

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    0/07

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4 0/09

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    02

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    04
    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    07

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    11
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    122

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    16
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    17
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    13

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    37
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    38
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    39
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    51

    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
    
    13

    >>> def numpy_gcd(a, b): ... a, b = np.broadcast_arrays(a, b) ... a = a.copy() ... b = b.copy() ... pos = np.nonzero(b)[0] ... while len(pos) > 0: ... b2 = b[pos] ... a[pos], b[pos] = b2, a[pos] % b2 ... pos = pos[b[pos]!=0] ... return a ... >>> numpy_gcd(np.array([98]), np.array([42])) array([14]) >>> 98/14, 42/14 (7, 3) 23 >>> def numpy_gcd(a, b): ... a, b = np.broadcast_arrays(a, b) ... a = a.copy() ... b = b.copy() ... pos = np.nonzero(b)[0] ... while len(pos) > 0: ... b2 = b[pos] ... a[pos], b[pos] = b2, a[pos] % b2 ... pos = pos[b[pos]!=0] ... return a ... >>> numpy_gcd(np.array([98]), np.array([42])) array([14]) >>> 98/14, 42/14 (7, 3) 24>>> def numpy_gcd(a, b): ... a, b = np.broadcast_arrays(a, b) ... a = a.copy() ... b = b.copy() ... pos = np.nonzero(b)[0] ... while len(pos) > 0: ... b2 = b[pos] ... a[pos], b[pos] = b2, a[pos] % b2 ... pos = pos[b[pos]!=0] ... return a ... >>> numpy_gcd(np.array([98]), np.array([42])) array([14]) >>> 98/14, 42/14 (7, 3) 25 >>> def numpy_gcd(a, b): ... a, b = np.broadcast_arrays(a, b) ... a = a.copy() ... b = b.copy() ... pos = np.nonzero(b)[0] ... while len(pos) > 0: ... b2 = b[pos] ... a[pos], b[pos] = b2, a[pos] % b2 ... pos = pos[b[pos]!=0] ... return a ... >>> numpy_gcd(np.array([98]), np.array([42])) array([14]) >>> 98/14, 42/14 (7, 3) 26

    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    27
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    28

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    30
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    31
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    32

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    19

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    23

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    25

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    34
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    31
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    34
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    37
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    37
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    8

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    41
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    31
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    41
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    37
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    37
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    8

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    5

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    7

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    0

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    4
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    5

    def simplify_fraction(numer, denom):
    
        if denom == 0:
            return "Division by 0 - result undefined"
    
        # Remove greatest common divisor:
        common_divisor = gcd(numer, denom)
        (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
        # Note that reduced_den > 0 as documented in the gcd function.
    
        if reduced_den == 1:
            return "%d/%d is simplified to %d" % (numer, denom, reduced_num)
        elif common_divisor == 1:
            return "%d/%d is already at its most simplified state" % (numer, denom)
        else:
            return "%d/%d is simplified to %d/%d" % (numer, denom, reduced_num, reduced_den)
    
    1

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    47

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    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
    
    49

    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    6
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    2

    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
    
    52

    Output:  :  

    Java

    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    7
    A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.
    
    
    def reducefract(n, d):
        '''Reduces fractions. n is the numerator and d the denominator.'''
        def gcd(n, d):
            while d != 0:
                t = d
                d = n%d
                n = t
            return n
        assert d!=0, "integer division by zero"
        assert isinstance(d, int), "must be int"
        assert isinstance(n, int), "must be int"
        greatest=gcd(n,d)
        n/=greatest
        d/=greatest
        return n, d
    
    8
    O(log(max(x,y)))

    Input : x = 16, y = 10
    Output : x = 8, y = 5
    
    Input : x = 10, y = 8
    Output : x = 5, y = 4
    0
    >>> def numpy_gcd(a, b):
    ...     a, b = np.broadcast_arrays(a, b)
    ...     a = a.copy()
    ...     b = b.copy()
    ...     pos = np.nonzero(b)[0]
    ...     while len(pos) > 0:
    ...         b2 = b[pos]
    ...         a[pos], b[pos] = b2, a[pos] % b2
    ...         pos = pos[b[pos]!=0]
    ...     return a
    ... 
    >>> numpy_gcd(np.array([98]), np.array([42]))
    array([14])
    >>> 98/14, 42/14
    (7, 3)
    
    9
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    0
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    2
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    1
    #find greatest common divisor for numerator and denominator, except 1:
    common_factor = 1
    for i in xrange(min(abs(numer), abs(denom)), 1, -1):
        if numer % i == 0 and denom % i == 0:
             common_factor = i
             break
    
    4
    O(log(max(x,y)))
     


    Làm thế nào để bạn biết nếu một phân số có thể được giảm?

    Một phân số được coi là đơn giản hóa nếu không có yếu tố chung, ngoài 1, trong tử số và mẫu số.Nếu một phân số có các yếu tố phổ biến trong tử số và mẫu số, chúng ta có thể giảm phân số xuống dạng đơn giản hóa của nó bằng cách loại bỏ các yếu tố phổ biến.if there are no common factors, other than 1 , in the numerator and denominator. If a fraction does have common factors in the numerator and denominator, we can reduce the fraction to its simplified form by removing the common factors.

    Làm thế nào để bạn kiểm tra phân số trong Python?

    Phân số (tử số = 0, mẫu số = 1): Điều này yêu cầu tử số và mẫu số là các trường hợp của số.Rational và một thể hiện phân số với value = (tử số/mẫu số) được trả về.Một lỗi Zerodivision được nêu ra nếu mẫu số = 0. : This requires that numerator and denominator are instances of numbers. Rational and a fraction instance with value = (numerator/denominator) is returned. A zerodivision error is raised if denominator = 0.

    Bạn có thể sử dụng phân số trong Python không?

    Từ Python 3.2 trở đi, bạn cũng có thể xây dựng một thể hiện phân số trực tiếp từ một số thập phân.you can also construct a Fraction instance directly from a decimal.