Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận : a = 12, b = 24
Output: 6
Explanation: all common divisors are 1, 2, 3, 4, 6 and 12
Input : a = 3, b = 17
Output: 1
Explanation: all common divisors are 1
Input : a = 20, b = 36
Output: 3
Explanation: all common divisors are 1, 2, 4
Đưa ra hai số nguyên, nhiệm vụ là tìm số lượng của tất cả các ước số chung của các số đã cho?
Đầu vào: A = 12, B = 24Output: 6Explanation: Tất cả các ước số chung là 1, 2, 3, 4, 6 và 12input: A = 3, B = 17Output: 36Output: 3Explanation: Tất cả các ước số chung là 1, 2, 4
Python
def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
4def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
5 def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
6def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
7def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
5 def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
9def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
0____25 def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
2def gcd[a,b]:
return a if not b else gcd[b, a%b]
9def gcd[a,b]:
while b:
a,b = b, a%b
return a
0gcd = lambda m,n: m if not n else gcd[n,m%n]
2def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
5def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
8def gcd[a,b]:
while b:
a,b = b, a%b
return a
4def gcd[a,b]:
while b:
a,b = b, a%b
return a
5
3 def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
4def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
5 def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
6def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
7def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
8def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
9def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
0gcd = lambda m,n: m if not n else gcd[n,m%n]
1__ O[min[a, b]], Where a and b is the given number.gcd = lambda m,n: m if not n else gcd[n,m%n]
Auxiliary
Space: O[1]
gcd = lambda m,n: m if not n else gcd[n,m%n]
5gcd = lambda m,n: m if not n else gcd[n,m%n]
6 gcd = lambda m,n: m if not n else gcd[n,m%n]
7gcd = lambda m,n: m if not n else gcd[n,m%n]
8gcd = lambda m,n: m if not n else gcd[n,m%n]
9def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
5def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
5____5222448gcd = lambda m,n: m if not n else gcd[n,m%n]
9__25________def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
2Ưu điểm chung lớn nhất [GCD] của A và B là con số lớn nhất phân chia cả hai đều không có phần còn lại.
Một cách để tìm GCD của hai số là thuật toán Euclid, dựa trên quan sát rằng nếu
def gcd[a,b]:
while b:
a,b = b, a%b
return a
6 là phần còn lại khi gcd = lambda m,n: m if not n else gcd[n,m%n]
7 được chia cho def gcd[a,b]:
return a if not b else gcd[b, a%b]
2, thì def gcd[a,b]:
while b:
a,b = b, a%b
return a
9. Như một trường hợp cơ sở, chúng ta có thể sử dụng gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
0.Viết một hàm gọi là GCD lấy các tham số
gcd = lambda m,n: m if not n else gcd[n,m%n]
7 và def gcd[a,b]:
return a if not b else gcd[b, a%b]
2 và trả về ước số chung lớn nhất của chúng.
Sampathsris
20.9k11 Huy hiệu vàng66 Huy hiệu bạc94 Huy hiệu đồng11 gold badges66 silver badges94 bronze badges
Hỏi ngày 24 tháng 6 năm 2012 lúc 5:13Jun 24, 2012 at 5:13
4
Nó nằm trong thư viện tiêu chuẩn.
>>> from fractions import gcd
>>> gcd[20,8]
4
Mã nguồn từ mô -đun
gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
3 trong Python 2.7:>>> print inspect.getsource[gcd]
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
Kể từ Python 3.5,
gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
4 nằm trong mô -đun gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
5; Một trong gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
6 không được dùng nữa. Hơn nữa, gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
7 không còn trả về mã nguồn giải thích cho một trong hai phương thức.Đã trả lời ngày 24 tháng 6 năm 2012 lúc 5:19Jun 24, 2012 at 5:19
user545424user545424user545424
15.3k11 Huy hiệu vàng53 Huy hiệu bạc69 Huy hiệu Đồng11 gold badges53 silver badges69 bronze badges
17
Các thuật toán với M-N có thể chạy rất dài.
Điều này thực hiện tốt hơn nhiều:
def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
Đã trả lời ngày 22 tháng 9 năm 2013 lúc 13:13Sep 22, 2013 at 13:13
Netomnetomnetom
3.2822 Huy hiệu vàng21 Huy hiệu bạc21 Huy hiệu đồng2 gold badges21 silver badges21 bronze badges
9
Phiên bản mã này sử dụng thuật toán của Euclid để tìm GCD.
def gcd_recursive[a, b]:
if b == 0:
return a
else:
return gcd_recursive[b, a % b]
Đã trả lời ngày 20 tháng 2 năm 2015 lúc 16:21Feb 20, 2015 at 16:21
AnkushankushAnkush
4404 Huy hiệu bạc8 Huy hiệu Đồng4 silver badges8 bronze badges
3
gcd = lambda m,n: m if not n else gcd[n,m%n]
Đã trả lời ngày 2 tháng 11 năm 2015 lúc 8:48Nov 2, 2015 at 8:48
Jonas Byströmjonas ByströmJonas Byström
24.3K22 Huy hiệu vàng98 Huy hiệu bạc142 Huy hiệu đồng22 gold badges98 silver badges142 bronze badges
0
sử dụng đệ quy,recursion,
def gcd[a,b]:
return a if not b else gcd[b, a%b]
sử dụng trong khi,while,
def gcd[a,b]:
while b:
a,b = b, a%b
return a
sử dụng lambda,
gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
Đã trả lời ngày 31 tháng 1 năm 2019 lúc 8:16Jan 31, 2019 at 8:16
1
def gcd[m,n]:
return gcd[abs[m-n], min[m, n]] if [m-n] else n
Đã trả lời ngày 29 tháng 6 năm 2013 lúc 5:48Jun 29, 2013 at 5:48
Dansalmodansalmodansalmo
11.2k5 Huy hiệu vàng56 Huy hiệu bạc51 Huy hiệu Đồng5 gold badges56 silver badges51 bronze badges
2
Giải pháp rất súc tích bằng cách sử dụng đệ quy:
def gcd[a, b]:
if b == 0:
return a
return gcd[b, a%b]
Đã trả lời ngày 16 tháng 5 năm 2018 lúc 12:02May 16, 2018 at 12:02
>>> print inspect.getsource[gcd]
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
0Một cách tiếp cận khác dựa trên thuật toán của Euclid.
Đã trả lời ngày 29 tháng 6 năm 2013 lúc 4:51Jun 29, 2013 at 4:51
>>> print inspect.getsource[gcd]
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
1
Marko Gresak
7.7315 huy hiệu vàng36 Huy hiệu bạc46 Huy hiệu đồng5 gold badges36 silver badges46 bronze badges
Đã trả lời ngày 15 tháng 11 năm 2013 lúc 9:56Nov 15, 2013 at 9:56
ShamsshamsSHAMS
Huy hiệu đồng 1911 bronze badge
Tôi nghĩ rằng một cách khác là sử dụng đệ quy. Đây là mã của tôi:
>>> print inspect.getsource[gcd]
def gcd[a, b]:
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b [so that when
b is divided by it, the result comes out positive].
"""
while b:
a, b = b, a%b
return a
2Đã trả lời ngày 27 tháng 10 năm 2015 lúc 14:13Oct 27, 2015 at 14:13
Dexhunterdexhunterdexhunter
5677 Huy hiệu bạc22 Huy hiệu Đồng7 silver badges22 bronze badges
1
trong Python với đệ quy:
>>> print inspect.getsource[gcd]
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
3Đã trả lời ngày 27 tháng 7 năm 2014 lúc 20:54Jul 27, 2014 at 20:54
>>> print inspect.getsource[gcd]
def gcd[a, b]:
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b [so that when
b is divided by it, the result comes out positive].
"""
while b:
a, b = b, a%b
return a
4
lennon310
12.2k11 Huy hiệu vàng41 Huy hiệu bạc60 Huy hiệu đồng11 gold badges41 silver badges60 bronze badges
Đã trả lời ngày 3 tháng 12 năm 2014 lúc 19:41Dec 3, 2014 at 19:41
Cho gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
8:
gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
>>> print inspect.getsource[gcd]
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
5Đối với gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
8 hoặc def gcd[m,n]:
return gcd[abs[m-n], min[m, n]] if [m-n] else n
0:
gcd = lambda a,b : a if not b else gcd[b, a%b]
>>> gcd[10,20]
>>> 10
def gcd[m,n]:
return gcd[abs[m-n], min[m, n]] if [m-n] else n
>>> print inspect.getsource[gcd]
def gcd[a, b]:
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b [so that when
b is divided by it, the result comes out positive].
"""
while b:
a, b = b, a%b
return a
6
Đã trả lời ngày 25 tháng 1 năm 2015 lúc 17:55Jan 25, 2015 at 17:55
2
Tôi đã phải làm một cái gì đó như thế này cho một bài tập về nhà bằng cách sử dụng các vòng lặp. Không phải là cách hiệu quả nhất, nhưng nếu bạn không muốn sử dụng một chức năng thì điều này hoạt động:
>>> print inspect.getsource[gcd]
def gcd[a, b]:
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b [so that when
b is divided by it, the result comes out positive].
"""
while b:
a, b = b, a%b
return a
7Đã trả lời ngày 16 tháng 4 năm 2019 lúc 16:21Apr 16, 2019 at 16:21
>>> print inspect.getsource[gcd]
def gcd[a, b]:
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b [so that when
b is divided by it, the result comes out positive].
"""
while b:
a, b = b, a%b
return a
8Đã trả lời ngày 7 tháng 6 năm 2019 lúc 16:24Jun 7, 2019 at 16:24
Sai Prateeksai PrateekSai prateek
Huy hiệu vàng 11.5k847 Huy hiệu bạc65 Huy hiệu đồng8 gold badges47 silver badges65 bronze badges
Mã này tính toán GCD của nhiều hơn hai số tùy thuộc vào lựa chọn được đưa ra bởi # người dùng, ở đây người dùng đưa ra số
>>> print inspect.getsource[gcd]
def gcd[a, b]:
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b [so that when
b is divided by it, the result comes out positive].
"""
while b:
a, b = b, a%b
return a
9Đã trả lời ngày 8 tháng 6 năm 2013 lúc 0:05Jun 8, 2013 at 0:05
1
Mã này tính toán GCD của nhiều hơn hai số tùy thuộc vào lựa chọn được đưa ra bởi # người dùng, ở đây người dùng đưa ra số
def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
0
Phân định
2.9014 Huy hiệu vàng31 Huy hiệu bạc38 Huy hiệu đồng4 gold badges31 silver badges38 bronze badges
Đã trả lời ngày 18 tháng 6 năm 2013 lúc 20:03Jun 18, 2013 at 20:03
Troychroitroychroitroychroi
491 Huy hiệu bạc9 Huy hiệu đồng1 silver badge9 bronze badges
2
def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
1greatest_common_devisor[A]
Đã trả lời ngày 25 tháng 3 năm 2015 lúc 16:23Mar 25, 2015 at 16:23
def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
2Đã trả lời ngày 25 tháng 5 năm 2018 lúc 12:47May 25, 2018 at 12:47
Par Baspar BasPar bas
2132 Huy hiệu bạc2 Huy hiệu đồng2 silver badges2 bronze badges
3
Đây là giải pháp thực hiện khái niệm
def gcd[m,n]:
return gcd[abs[m-n], min[m, n]] if [m-n] else n
1:def gcd[x, y]:
while y != 0:
[x, y] = [y, x % y]
return x
3Đã trả lời ngày 15 tháng 2 năm 2019 lúc 21:40Feb 15, 2019 at 21:40