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
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.
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.
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:
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.
#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)
95def 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 fractions
0def 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 fractions
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 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)
95def 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 fractions
0def 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 fractions
2 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
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
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
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
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
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
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
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
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
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
8def 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)
0def 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
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
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
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
5def 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)
1Java
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
5Input : 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
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
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
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
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
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
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
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
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
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
8def 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)
0def 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
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
5def 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)
1Java
#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
5A 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
8Input : 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
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
2def 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)
1def 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)
1Python3
#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
6x = 8, y = 5
8def 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 fractions
0def 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 fractions
2Input : 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 fractions
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 fractions
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
1 0/0
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
60/0
3 0/0
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)
00/0
60/0
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
4 0/0
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
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 >>> 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
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
2C#
>>> 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)
26A 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
5Input : 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
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
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
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
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
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
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
6x = 8, y = 5
8def 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 fractions
0def 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 fractions
2def 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
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
5def 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)
1Java
#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
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
2def 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)
1def 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
34Java
#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)
13A 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
8Input : 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
6x = 8, y = 5
8def 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 fractions
0def 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 fractions
2Input : 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 fractions
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 fractions
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
1 0/0
0def 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/0
3 0/0
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)
00/0
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
50/0
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
4 0/0
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
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 >>> 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)
07def 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
51def 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
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
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
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
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
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
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
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
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
8def 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
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
5def 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
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
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
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
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
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
2def 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
52Output: :
JavaA 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
4O(log(max(x,y)))
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.
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.