LCM của hai số trong Python mà không cần sử dụng Hàm

Đây là một phương pháp bình thường để tìm lcm của hai số trong python. Chúng tôi sẽ lấy hai số trong khi khai báo các biến. Chương trình Python tìm lcm của hai số bằng câu lệnh if-else và vòng lặp while

# Python program to find the LCM of the two numbers

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# choose the greater number
if (num1 > num2):
    greater = num1
else:
    greater = num2

while(True):
    # find LCM
    if(greater % num1 == 0 and greater % num2 == 0):
        print('The LCM of',num1,'and',num2,'is',greater)
        break
    greater += 1

Đầu ra cho giá trị đầu vào khác nhau. -

Nhập số đầu tiên. 2
Nhập số thứ hai. 4
BCNN của 2 và 4 là 4

Nhập số đầu tiên. 3
Nhập số thứ hai. 5
LCM của 3 và 5 là 15

Nhập số đầu tiên. 20
Nhập số thứ hai. số 8
LCM của 20 và 8 là 40

Trong mỗi lần lặp lại, chúng tôi kiểm tra xem cả hai số có chia hoàn toàn số của chúng tôi không. Nếu vậy, chúng tôi lưu trữ số dưới dạng L. C. M. và thoát khỏi vòng lặp. Mặt khác, số được tăng thêm 1 và vòng lặp tiếp tục

Hàm LCM trong Python

Chúng ta cũng có thể nhờ sự trợ giúp của hàm tìm lcm của hai số trong python. Hàm là một khối mã thực hiện một tác vụ cụ thể

# Python program to find the LCM using function

def find_lcm(a, b):   #user-defined function
   # choose the greater number
   if a > b:
       greater = a
   else:
       greater = b

   while(True):
       # find LCM
       if((greater % a == 0) and (greater % b == 0)):
           lcm = greater
           break
       greater += 1
   return lcm

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calling function & display result
print('The LCM of',num1,'and',num2,'is',find_lcm(num1, num2))

đầu ra. -

Nhập số đầu tiên. 50
Nhập số thứ hai. 40
LCM của 50 và 40 là 200

Chương trình sử dụng GCD

Các phương thức chương trình trên chạy chậm hơn. Chúng ta có thể làm cho nó hiệu quả hơn và nhanh hơn bằng cách sử dụng thực tế là tích của hai số a và b bằng tích của HCF(a,b) và LCM(a,b)

a*b = HCF(a, b) * LCM(a, b)

HCF (nhân tố chung cao nhất) còn được gọi là GCD (Đo lường chung lớn nhất), Sử dụng công thức này, chúng ta có thể tìm thấy GCD và LCM tại một thời điểm. Chúng ta cần tìm GCD và LCM rồi áp dụng công thức này

Trong chương trình dưới đây để tìm LCM của hai số trong python; . Công thức được sử dụng cho mục đích này là. -

LCM(a, b) = (a*b) / HCF(a, b)

# Python program to find the LCM using GCD

# This function find GCD 
def find_gcd(a, b):
    while(b):
        a, b = b, a % b
    return a

# This function find LCM
def find_lcm(a, b):
    lcm = (a*b)//find_gcd(a,b)
    return lcm

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calling function & display result
print('The LCM of',num1,'and',num2,'is',find_lcm(num1, num2))

đầu ra. -

Nhập số đầu tiên. 10
Nhập số thứ hai. 25
LCM của 10 và 25 là 50

LCM của hai số trong Python sử dụng đệ quy

Chúng ta cũng có thể sử dụng kỹ thuật đệ quy để tìm lcm của hai số. Một kỹ thuật xác định phương thức/hàm chứa lệnh gọi đến chính nó được gọi là đệ quy. Hàm/phương thức đệ quy cho phép chúng ta chia vấn đề phức tạp thành các trường hợp đơn giản giống hệt nhau có thể xử lý dễ dàng. Đây cũng là một kỹ thuật lập trình máy tính nổi tiếng. phân chia và chinh phục

# Python program to find the LCM using recursion

# This recursive function find GCD 
def find_gcd(a, b):
    if(b == 0):
        return a
    else:
        return find_gcd(b, a%b)

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# find LCM
lcm = (num1 * num2) // find_gcd(num1, num2)

# display result
print('The LCM of',num1,'and',num2,'is',lcm)

đầu ra. -

Nhập số đầu tiên. 9
Nhập số thứ hai. 31
LCM của 9 và 31 là 279

Cũng thấy. - Tìm giai thừa của một số trong Python

Nếu bạn thích bài đăng này, hãy chia sẻ nó với bạn bè của bạn. Bạn có muốn chia sẻ thêm thông tin về chủ đề đã thảo luận ở trên hay bạn có thấy điều gì không đúng không? . Cảm ơn bạn

LCM (Bội chung nhỏ nhất) của hai số là số nhỏ nhất chia hết cho cả hai số.  

Ví dụ: LCM của 15 và 20 là 60 và LCM của 5 và 7 là 35

Một giải pháp đơn giản là tìm tất cả các thừa số nguyên tố của cả hai số, sau đó tìm hợp của tất cả các thừa số có trong cả hai số. Cuối cùng, trả về sản phẩm của các yếu tố trong hiệp hội

Một giải pháp hiệu quả dựa trên công thức dưới đây cho LCM của hai số 'a' và 'b'.  

   a x b = LCM(a, b) * GCD (a, b)

   LCM(a, b) = (a x b) / GCD(a, b)

Chúng ta đã thảo luận về chức năng tìm GCD của hai số. Sử dụng GCD, chúng ta có thể tìm thấy LCM.  

Dưới đây là triển khai ý tưởng trên

C++




// C++ program to find LCM of two numbers

#include

using namespace std;

 

// Recursive function to return gcd of a and b

long long

LCM of 15 and 20 is 60
0long long
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
4long long
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
8

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers0// C++ program to find LCM of two numbers1 // C++ program to find LCM of two numbers2

// C++ program to find LCM of two numbers3____142_______4 // C++ program to find LCM of two numbers5

// C++ program to find LCM of two numbers0// C++ program to find LCM of two numbers4 // C++ program to find LCM of two numbers8

// C++ program to find LCM of two numbers9

 

#include 0

long long #include 3_______5_______3

LCM of 15 and 20 is 60
4
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
8

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4 using1

// C++ program to find LCM of two numbers9

using3

using4

LCM of 15 and 20 is 60
3 using6

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3____5_______3 namespace0

// C++ program to find LCM of two numbers3namespace2namespace3 namespace4namespace5

namespace6namespace7namespace8 namespace9

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4 std;2

// C++ program to find LCM of two numbers9

C




std;4

std;5

 

// Recursive function to return gcd of a and b

LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
0_______5_______3
LCM of 15 and 20 is 60
4
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
8

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers1 // Recursive function to return gcd of a and b6

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers4 // Recursive function to return gcd of a and b9

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4 long2

// C++ program to find LCM of two numbers9

 

#include 0

LCM of 15 and 20 is 60
3 #include 3_______5_______3
LCM of 15 and 20 is 60
4_______5_______3
LCM of 15 and 20 is 60
8

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4 using1

// C++ program to find LCM of two numbers9

 

using4

LCM of 15 and 20 is 60
3 using6

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3____5_______3 namespace0

// C++ program to find LCM of two numbers3_______5_______04

LCM of 15 and 20 is 60
05
LCM of 15 and 20 is 60
06
LCM of 15 and 20 is 60
07

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4 std;2

// C++ program to find LCM of two numbers9

Java




LCM of 15 and 20 is 60
12

LCM of 15 and 20 is 60
13
LCM of 15 and 20 is 60
14

LCM of 15 and 20 is 60
15
LCM of 15 and 20 is 60
16
LCM of 15 and 20 is 60
17

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3____5_______20

// C++ program to find LCM of two numbers3

LCM of 15 and 20 is 60
22
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
0
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
4
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
8

// C++ program to find LCM of two numbers3____5_______9

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers1

LCM of 15 and 20 is 60
33
LCM of 15 and 20 is 60
34
LCM of 15 and 20 is 60
35

LCM of 15 and 20 is 60
36// C++ program to find LCM of two numbers4 // Recursive function to return gcd of a and b9

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers4 long2

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers9

// C++ program to find LCM of two numbers3

// C++ program to find LCM of two numbers3____5_______46

// C++ program to find LCM of two numbers3

LCM of 15 and 20 is 60
22
LCM of 15 and 20 is 60
3 #include 3
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
4
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
8

// C++ program to find LCM of two numbers3____5_______9

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers4 using1

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers9

// C++ program to find LCM of two numbers3

// C++ program to find LCM of two numbers3____5_______64

// C++ program to find LCM of two numbers3

LCM of 15 and 20 is 60
15
LCM of 15 and 20 is 60
22
LCM of 15 and 20 is 60
68
LCM of 15 and 20 is 60
69

// C++ program to find LCM of two numbers3____5_______9

// Recursive function to return gcd of a and b7

LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
74
LCM of 15 and 20 is 60
75
LCM of 15 and 20 is 60
76
LCM of 15 and 20 is 60
77
LCM of 15 and 20 is 60
78

// Recursive function to return gcd of a and b7

LCM of 15 and 20 is 60
80namespace3
LCM of 15 and 20 is 60
82

LCM of 15 and 20 is 60
83namespace5
LCM of 15 and 20 is 60
85

LCM of 15 and 20 is 60
86_______145_______8
LCM of 15 and 20 is 60
88

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers9

// C++ program to find LCM of two numbers9

Python3




LCM of 15 and 20 is 60
92

 

LCM of 15 and 20 is 60
93

LCM of 15 and 20 is 60
94
LCM of 15 and 20 is 60
95

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers1

LCM of 15 and 20 is 60
98
LCM of 15 and 20 is 60
99
LCM of 15 and 20 is 60
99
LCM of 15 and 20 is 60
34// C++ program to find LCM of two numbers02

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers4 // C++ program to find LCM of two numbers05

// C++ program to find LCM of two numbers3____142_______4 // C++ program to find LCM of two numbers08// C++ program to find LCM of two numbers09 // C++ program to find LCM of two numbers10

 

// C++ program to find LCM of two numbers11

LCM of 15 and 20 is 60
94 // C++ program to find LCM of two numbers13

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4 // C++ program to find LCM of two numbers16// C++ program to find LCM of two numbers17// C++ program to find LCM of two numbers17 // C++ program to find LCM of two numbers19// C++ program to find LCM of two numbers20 // C++ program to find LCM of two numbers05

 

// C++ program to find LCM of two numbers22

LCM of 15 and 20 is 60
98_______5_______99
LCM of 15 and 20 is 60
75

// C++ program to find LCM of two numbers05_______5_______99

LCM of 15 and 20 is 60
77

// C++ program to find LCM of two numbers29

LCM of 15 and 20 is 60
05// C++ program to find LCM of two numbers31// C++ program to find LCM of two numbers32// C++ program to find LCM of two numbers33// C++ program to find LCM of two numbers34// C++ program to find LCM of two numbers35// C++ program to find LCM of two numbers36

 

// C++ program to find LCM of two numbers37

C#




// C++ program to find LCM of two numbers38

// C++ program to find LCM of two numbers39

using // C++ program to find LCM of two numbers41

LCM of 15 and 20 is 60
16 // C++ program to find LCM of two numbers43

// C++ program to find LCM of two numbers3

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers46

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers48

// C++ program to find LCM of two numbers3

LCM of 15 and 20 is 60
22
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
0
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
4
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
8

// C++ program to find LCM of two numbers3____5_______9

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers1 // Recursive function to return gcd of a and b6

LCM of 15 and 20 is 60
36// C++ program to find LCM of two numbers4 // Recursive function to return gcd of a and b9

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers4 long2

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers9

// C++ program to find LCM of two numbers3

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers72

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers74

// C++ program to find LCM of two numbers3

LCM of 15 and 20 is 60
22
LCM of 15 and 20 is 60
3 #include 3
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
4
LCM of 15 and 20 is 60
3
LCM of 15 and 20 is 60
8

// C++ program to find LCM of two numbers3____5_______9

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers4 using1

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers9

// C++ program to find LCM of two numbers3

// C++ program to find LCM of two numbers3____5_______64

// C++ program to find LCM of two numbers3____5_______15

LCM of 15 and 20 is 60
22
LCM of 15 and 20 is 60
68 // C++ program to find LCM of two numbers97

// C++ program to find LCM of two numbers3____5_______9

// Recursive function to return gcd of a and b7

LCM of 15 and 20 is 60
3 namespace0

// Recursive function to return gcd of a and b7#include 04namespace3

LCM of 15 and 20 is 60
82

________145 _______6________145 ______5

LCM of 15 and 20 is 60
85 ________145 _______8
LCM of 15 and 20 is 60
88

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers9

// C++ program to find LCM of two numbers9

 

#include 15

PHP




#include 16

#include 17

 

#include 18

// C++ program to find LCM of two numbers48

#include 20

LCM of 15 and 20 is 60
0#include 22#include 23#include 24
LCM of 15 and 20 is 60
35

LCM of 15 and 20 is 60
9

#include 27// C++ program to find LCM of two numbers1

LCM of 15 and 20 is 60
05#include 22 #include 31

// Recursive function to return gcd of a and b7// C++ program to find LCM of two numbers4 #include 24

LCM of 15 and 20 is 60
78

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4

LCM of 15 and 20 is 60
0#include 24 // C++ program to find LCM of two numbers09#include 22#include 23#include 22#include 44

// C++ program to find LCM of two numbers9

 

#include 46

#include 47

#include 20 #include 3#include 22#include 23#include 24

LCM of 15 and 20 is 60
35

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4

LCM of 15 and 20 is 60
05#include 22 #include 59#include 22#include 23#include 24#include 63#include 24
LCM of 15 and 20 is 60
78

// C++ program to find LCM of two numbers9

 

// C++ program to find LCM of two numbers3#include 68

// C++ program to find LCM of two numbers3#include 22 #include 71

// C++ program to find LCM of two numbers3#include 24 #include 74

// C++ program to find LCM of two numbers3#include 76 namespace3#include 23#include 22#include 23namespace5

namespace6#include 23#include 24#include 23namespace8#include 87#include 22#include 23#include 24#include 44

 

#include 15

#include 93

Javascript




#include 94

 

#include 95

 

// Recursive function to return gcd of a and b

#include 20 #include 98

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers1 // C++ program to find LCM of two numbers2

// C++ program to find LCM of two numbers3____142_______4 // C++ program to find LCM of two numbers5

// C++ program to find LCM of two numbers4 // C++ program to find LCM of two numbers8

// C++ program to find LCM of two numbers9

 

#include 0

#include 20 using10

LCM of 15 and 20 is 60
9

// C++ program to find LCM of two numbers3// C++ program to find LCM of two numbers4 using1

// C++ program to find LCM of two numbers9

 

using4

using3

// C++ program to find LCM of two numbers3using19

// C++ program to find LCM of two numbers3using21____145_______3

LCM of 15 and 20 is 60
82namespace5

// C++ program to find LCM of two numbers3_______5_______85namespace8

LCM of 15 and 20 is 60
88

// C++ program to find LCM of two numbers3

 

using30

 

using31

Đầu ra

LCM of 15 and 20 is 60

Thời gian phức tạp. O(log(min(a,b))

Không gian phụ trợ. O(log(min(a,b))

https. //youtube. be/anSfYgbo694

Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên

Làm cách nào để tính bội số chung nhỏ nhất của hai số nguyên trong Python?

Chúng ta có thể làm cho nó hiệu quả hơn bằng cách sử dụng thực tế là tích của hai số bằng tích của bội chung nhỏ nhất và ước chung lớn nhất của hai số đó. Số1 * Số2 = L. C. M. *G. C. D

Có chức năng nào cho LCM trong Python không?

lcm() có sẵn trong Python 3. 9. 0 trở lên .

Cách dễ nhất để tìm LCM của hai số là gì?

Phương pháp tìm LCM . Đầu tiên liệt kê các thừa số nguyên tố của mỗi số. Step 1: To first list the prime factors of each number. Bước 2. Tiếp theo nhân mỗi thừa số với số lần tối đa nó xuất hiện ở một trong hai số. Nếu cùng một thừa số xuất hiện nhiều lần trong cả hai số, thì nhân thừa số với số lần xuất hiện lớn nhất.