forked from Vishalrai2002/CP_Math_CB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006Gcd.cpp
More file actions
34 lines (23 loc) · 743 Bytes
/
006Gcd.cpp
File metadata and controls
34 lines (23 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<algorithm>
using namespace std;
// here we find our the gcd by using long divison method
// divide until your reminder not becomes zero and if your remainde is non zero at some point
// the you make diviosor to divident and remainder to divisor.
// Time complexity log(N)
int gcd(int a,int b){
if(b==0) return a; // base case
return gcd(b,a%b); // recursive case
}
int main(){
// gcd
cout<<gcd(4,12)<<endl;
cout<<gcd(18,12)<<endl;
// we can find out the LCM by the help of relatoinship between the gcd and lcm
// a*b/gcd=lcm
// lcm
cout<<18*12/gcd(18,12)<<endl;
// cout<<__gcd(4,12)<<endl; // Time COmplexity is LOG(N)
// __gcd(a,__gcd(b,c));
return 0;
}