File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ // C++ program to find the LCM using the HCF of the numbers
2+ // This algorithm finds LCM of two numbers in logarithmic time complexity
3+ #include < bits/stdc++.h>
4+ using namespace std ;
5+ // Function to compute the HCF, to further use in finding the LCM
6+
7+ int Find_HCF (int a, int b){
8+ while (a>0 && b>0 ){
9+ // If a > b then a%b
10+ if (a>b){
11+ a = a%b;
12+ }
13+ // Else b%a
14+ else {
15+ b = b%a;
16+ }
17+ }
18+ // When the loop exhausts, the non zero number will be the HCF
19+ if (a==0 ){
20+ return b;
21+ }
22+ else {
23+ return a;
24+ }
25+ }
26+ int Find_LCM (int a, int b){
27+ // Using the formula lcm=a*b/hcf(a,b),
28+ return a*b/Find_HCF (a,b);
29+ }
30+
31+ int main (){
32+ // Input two numbers
33+ int a,b;
34+ cin >> a >> b;
35+ // Calling Find_LCM function to find the LCM,
36+ cout << Find_LCM (a,b) ;
37+ }
38+
39+
40+ // Time Complexity = O(log(min(a,b)))
41+ // Space Complexity = O(1)
You can’t perform that action at this time.
0 commit comments