Phương pháp toán học
Thí dụ
Tìm ước số chung lớn nhất của hai số nguyên:
#Trình thư viện toán học Mathimport Toán học
import math
#find the & nbsp; Ưu điểm chung lớn nhất của hai integersprint (math.gcd (3, 6)) in (math.gcd (6, 12)) in (math.gcd (12, 36)) in (Math.gcd (-12, - 36)) In (Math.gcd (5, 12)) In (Math.gcd (10, 0)) in (Math.gcd (0, 34)) in (Math.gcd (0, 0))
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))
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương pháp math.gcd() trả về ước số chung lớn nhất của hai số nguyên Int1 và Int2.
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ị int, đạ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
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 : 129The gcd of 60 and 48 is : 120
if(b ____101010 The gcd of 60 and 48 is : 122The gcd of 60 and 48 is : 123
The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 123The gcd of 60 and 48 is : 124 The gcd of 60 and 48 is : 125
math.gcd()2math.gcd()3math.gcd()4math.gcd()5The gcd of 60 and 48 is : 120math.gcd()7
math.gcd()2math.gcd()9The gcd of 60 and 48 is : 128int1math.gcd()1int3
The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 128
The gcd of 60 and 48 is : 12The gcd of 60 and 48 is : 129____10 math.gcd()1Using Loops
Python3
Đầu ra
Cách 2: Sử dụng các vòng & NBSP;
def int5
The gcd of 60 and 48 is : 129The gcd of 60 and 48 is : 120
if int8
The gcd of 60 and 48 is : 124def0____10 def2
The gcd of 60 and 48 is : 124def0____10 def9
hcfnaive(a, b):1 hcfnaive(a, b):2hcfnaive(a, b):3 hcfnaive(a, b):4__
The gcd of 60 and 48 is : 124if 3__
The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 123The gcd of 60 and 48 is : 124 The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 128
The gcd of 60 and 48 is : 129____10 math.gcd()1
The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 128
The gcd of 60 and 48 is : 12The gcd of 60 and 48 is : 129____10 math.gcd()1Using Euclidean Algorithm
Python3
Đầu ra
Cách 2: Sử dụng các vòng & NBSP;
def int5
if int8
The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 123The gcd of 60 and 48 is : 124 The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 128
The gcd of 60 and 48 is : 129____10 math.gcd()1
Output:
The gcd of 60 and 48 is : 12- Đầu ra
- Cách 2: Sử dụng các vòng & NBSP;