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
Đọcmath module and hence can make tasks easier in many situations.
Bàn luận
Yếu tố chung cao nhất [HCF], còn được gọi là GCD, có thể được tính toán trong Python bằng cách sử dụng một hàm duy nhất được cung cấp bởi mô -đun toán học và do đó có thể giúp các nhiệm vụ dễ dàng hơn trong nhiều tình huống. Using Recursion
Python3
Phương pháp ngây thơ để tính toán GCD
Cách 1: Sử dụng đệ quy
def
hcfnaive[a, b]:
The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 128
if
[b
___
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 124
The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
def
0def
1def
2def
3=
def
5
def
0def
7
The gcd of 60 and 48 is : 126
def
9The gcd of 60 and 48 is : 129
hcfnaive[a, b]:
1The gcd of 60 and 48 is : 12
4=
The gcd of 60 and 48 is : 12
6
The gcd of 60 and 48 is : 12
The gcd of 60 and 48 is : 12
7=
The gcd of 60 and 48 is : 12
9Using Loops
Python3
Đầu ra
Cách 2: Sử dụng các vòng & NBSP;
def
hcfnaive[a, b]:
3
The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 128
if
hcfnaive[a, b]:
6
The gcd of 60 and 48 is : 122
hcfnaive[a, b]:
8=
0The gcd of 60 and 48 is : 122
hcfnaive[a, b]:
8=
7
9 if
0if
1
The gcd of 60 and 48 is : 122
if
[b
1The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 124
The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 124
=
The gcd of 60 and 48 is : 126
The gcd of 60 and 48 is : 127
=
The gcd of 60 and 48 is : 129
The gcd of 60 and 48 is : 12
4=
The gcd of 60 and 48 is : 12
6
The gcd of 60 and 48 is : 12
The gcd of 60 and 48 is : 12
7=
The gcd of 60 and 48 is : 12
9Using Euclidean Algorithm
Python3
Đầu ra
Cách 2: Sử dụng các vòng & NBSP;
def
hcfnaive[a, b]:
3
if
hcfnaive[a, b]:
6
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 124
The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 122
The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 124
=
The gcd of 60 and 48 is : 126
The gcd of 60 and 48 is : 127
=
The gcd of 60 and 48 is : 129
Output:
The gcd of 60 and 48 is : 12
- Đầu ra
- Cách 2: Sử dụng các vòng & NBSP;
Phương pháp toán học
Làm thế nào để bạn sử dụng GCD trong Python?
Hàm gcd [] Tính toán ước số chung lớn nhất của 2 số được đề cập trong các đối số của nó ..
Cú pháp: Math.gcd [x, y].
import math
X: Số nguyên không âm có GCD phải được tính toán ..
print [math.gcd[3, 6]]
print [math.gcd[6, 12]]
print [math.gcd[12, 36]]
print [math.gcd[-12, -36]]
print [math.gcd[5, 12]]
print [math.gcd[10, 0]]
print
[math.gcd[0, 34]]
print [math.gcd[0, 0]]
Y: Số nguyên không âm có GCD phải được tính toán ..
Python có chức năng GCD không?
Ưu điểm phổ biến lớn nhất hoặc GCD là một biểu thức toán học để tìm số lượng cao nhất có thể chia cả hai số mà GCD phải tìm thấy với phần còn lại là không. Nó có nhiều ứng dụng toán học. Python có chức năng GCD sẵn có trong mô -đun toán học có thể được sử dụng cho mục đích này.
GCD là ước số chung lớn nhất phân chia các số mà không có phần còn lại.
GCD còn được gọi là yếu tố chung cao nhất [HCF].
Mẹo: GCD [0,0] Trả về 0. gcd[0,0] returns 0.
Cú pháp
Giá trị tham số
int1 | Yêu cầu. Số nguyên đầu tiên tìm GCD cho |
Int2 | Yêu cầu. Số nguyên thứ hai để tìm GCD cho |
Chi tiết kỹ thuật
Giá trị The gcd of 60 and 48 is : 1245, đại diện cho ước số chung [GCD] lớn nhất cho hai số nguyên |
3.5 |
Phương pháp toán học