Python Program - Find LCM of Two Numbers without GCD
LCM stands for Least Common Multiple. The LCM of two numbers is the smallest number that can be divided by both numbers.
For example - LCM of 20 and 25 is 100 and LCM of 30 and 40 is 120.
The approach is to start with the largest of the two numbers and keep incrementing the larger number by itself till it becomes divisible by the smaller number.
Example: find LCM of two numbers without GCD
In the example below, to calculate the LCM of two numbers, largest of the two numbers is increased by itself till it becomes divisible by the smaller number.
def lcm(x, y): large = max(x, y) small = min(x, y) i = large while(True): if(i % small == 0): return i i = i + large x = 30 y = 40 print("LCM of", x, "and", y, "is:", lcm(x,y))
The above code will give the following output:
LCM of 30 and 40 is: 120
Recommended Pages
- Python Program - To Check Prime Number
- Python Program - Bubble Sort
- Python Program - Selection Sort
- Python Program - Maximum Subarray Sum
- Python Program - Reverse digits of a given Integer
- Python - Swap two numbers
- Python Program - Fibonacci Sequence
- Python Program - Insertion Sort
- Python Program - Find Factorial of a Number
- Python Program - Find HCF of Two Numbers
- Python Program - Merge Sort
- Python Program - Shell Sort
- Stack in Python
- Queue in Python
- Python Program - Find LCM of Two Numbers
- Python Program - To Check Armstrong Number
- Python Program - Counting Sort
- Python Program - Radix Sort
- Python Program - Find Largest Number among Three Numbers
- Python Program - Print Floyd's Triangle