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
a
=
12
b
=
24
n
=
The gcd of 60 and 48 is : 121
a
8a
9
GCD of 98 and 56 is 141
=
The gcd of 60 and 48 is : 127
=
3=
4
The gcd of 60 and 48 is : 12
2 The gcd of 60 and 48 is : 12
3The gcd of 60 and 48 is : 12
4 The gcd of 60 and 48 is : 12
5The gcd of 60 and 48 is : 12
6The gcd of 60 and 48 is : 12
7The gcd of 60 and 48 is : 12
8__ O[min[a, b]], Where a and b is the given number.
Auxiliary
Space: O[1]
Các
GCD of 98 and 56 is 14
4b
9 The gcd of 60 and 48 is : 12
38The gcd of 60 and 48 is : 12
34The gcd of 60 and 48 is : 12
40
a
=
The gcd of 60 and 48 is : 12
43
b
=
The gcd of 60 and 48 is : 12
46
GCD of 98 and 56 is 14
4b
9 The gcd of 60 and 48 is : 12
38The gcd of 60 and 48 is : 12
34The gcd of 60 and 48 is : 12
40
a
=
The gcd of 60 and 48 is : 12
43
b
=
The gcd of 60 and 48 is : 12
46
Làm thế nào để bạn tìm thấy các ước số chung của hai số trong Python?
Phương pháp tìm các ước số chung của hai số.
Lưu trữ hai số trong biến 'num1' và 'num2' ..
Cú pháp: math.gcd [x, y] math.gcd[x, y]
Parameter:
X: Số nguyên không âm có GCD phải được tính toán.: Non-negative integer whose gcd has to be computed.
Y: Số nguyên không âm có GCD phải được tính toán.: Non-negative integer whose gcd has to be computed.
Trả về: Giá trị số nguyên tuyệt đối/dương sau khi tính toán GCD của các tham số đã cho X và Y. An absolute/positive integer value after calculating the GCD of given parameters x and y.
Ngoại lệ: Khi cả x và y là 0, hàm trả về 0, nếu bất kỳ số nào là ký tự, loại lỗi được nêu ra. When Both x and y are 0, function returns 0, If any number is a character, Type error is raised.
Python3
=
5 =
6
=
3
The gcd of 60 and 48 is : 126
=
912
0=
12
2=
312
412
5
The gcd of 60 and 48 is : 128
12
712
8Đầu ra
The gcd of 60 and 48 is : 12
Sử dụng đệ quy:
Python3
12
9 b
0
GCD of 98 and 56 is 144
GCD of 98 and 56 is 145
b
3=
=
The gcd of 60 and 48 is : 121
GCD of 98 and 56 is 143
a
8b
9
GCD of 98 and 56 is 146
GCD of 98 and 56 is 144
=
2a
7a
8b
9 =
6
GCD of 98 and 56 is 147
=
8a
=
12
5
b
=
12
7
=
3
The gcd of 60 and 48 is : 126
=
912
0=
12
2=
3n
212
5
The gcd of 60 and 48 is : 128
12
712
8Đầu ra
The gcd of 60 and 48 is : 12
Sử dụng thuật toán Euclide:
Thuật toán Euclid [hoặc thuật toán Euclide] là một phương pháp để tìm kiếm hiệu quả các ước số chung [GCD] một cách hiệu quả. GCD của hai số nguyên X và Y là số lớn nhất phân chia cả X và Y [không để lại phần còn lại].
Mã giả của thuật toán-
- Đặt & nbsp; a, b & nbsp; là hai số
- a mod b = r
- Let & nbsp; a = b & nbsp; và & nbsp; b = r
- Lặp lại các bước 2 và 3 cho đến khi & nbsp; mod b & nbsp; lớn hơn 0
- Gcd = b
- Finish
Python3
12
9 n
8
GCD of 98 and 56 is 144
GCD of 98 and 56 is 145
The gcd of 60 and 48 is : 1201
=
=
The gcd of 60 and 48 is : 121
GCD of 98 and 56 is 143
a
8b
9 a
1
GCD of 98 and 56 is 144
GCD of 98 and 56 is 145
b
3=
=
The gcd of 60 and 48 is : 121
GCD of 98 and 56 is 143
a
8b
9
GCD of 98 and 56 is 146
GCD of 98 and 56 is 144
GCD of 98 and 56 is 145
The gcd of 60 and 48 is : 1201
=
=
The gcd of 60 and 48 is : 1224
a
8b
9
GCD of 98 and 56 is 146
GCD of 98 and 56 is 144
GCD of 98 and 56 is 145
The gcd of 60 and 48 is : 1201
=
=
The gcd of 60 and 48 is : 1224
GCD of 98 and 56 is 144
GCD of 98 and 56 is 145
The gcd of 60 and 48 is : 1230
a
8b
9
The gcd of 60 and 48 is : 1233
The gcd of 60 and 48 is : 1234
The gcd of 60 and 48 is : 1235
GCD of 98 and 56 is 144
b
9 The gcd of 60 and 48 is : 1238
The gcd of 60 and 48 is : 1234
The gcd of 60 and 48 is : 1240
a
=
The gcd of 60 and 48 is : 1243
GCD of 98 and 56 is 145
The gcd of 60 and 48 is : 1248
GCD of 98 and 56 is 144
=
3The gcd of 60 and 48 is : 126
The gcd of 60 and 48 is : 1252
The gcd of 60 and 48 is : 1253
The gcd of 60 and 48 is : 1254
The gcd of 60 and 48 is : 1255
The gcd of 60 and 48 is : 1256
The gcd of 60 and 48 is : 1257
=
2a
7
GCD of 98 and 56 is 144
=
3The gcd of 60 and 48 is : 126
The gcd of 60 and 48 is : 1263
The gcd of 60 and 48 is : 1264
Đầu ra
GCD of 98 and 56 is 14