-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53.cpp
More file actions
126 lines (94 loc) · 2.46 KB
/
Copy path53.cpp
File metadata and controls
126 lines (94 loc) · 2.46 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <bits/stdc++.h>
using namespace std;
// GCD and LCM using Euclid's Algorithm With Applications
// GCD - greatest common divisor (HCF - highest common factor)
// LCM - least common multiple
/*
4 and 12 -> GCD = 4
we write prime factorization for each and
4 = 2^2 * 3^0
12 = 2^2 * 3^1
for GCD extract min. powers
thus 2^2*3^0 = 4
for LCM extract max. powers
thus 2^2 * 3^1 = 12
similarly for 12 and 18
12 = 2^2 * 3^1
18 = 2^1 * 3^2
GCD = 2^1 * 3^1 = 6
LCM = 2^2 * 3^2 = 36
****** Relationship b/w GCD and LCM
(a * b)/GCD = LCM
ex. for 12 and 18
2^2 * 2^1 * 3^1* 3^2
--------------------- = 2^2 * 3^1
2^1 * 3^1
thus if we get GCD then we can give LCM in O(1) time
### Euclid's algorithm
lets start with 4 and 12 only
divide 12 with 4 until remainder is 0 at that point the divisor is GCD
i.e. long division this is only euclid's algorithm
3
______
4 | 12
12
------
0
thus GCD is 4
for 12 and 18
1
______
12 | 18
12 2
----- _______
6 | 12
12
---- _____
0 | 6
thus GCD is 6
in case we divide 12 with 18
thus only step extra added before
0
___
18 | 12
0 1
---- ______
12 | 18
12 2
----- _______
6 | 12
12
---- _____
0 | 6
*/
int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a%b);
}
// time complexity is less than O(log(n))
int main() {
cout<<gcd(4, 12)<<endl;
cout<<gcd(12, 4)<<endl;
cout<<gcd(12, 0)<<endl;
cout<<gcd(12, 1)<<endl;
cout<<gcd(12, 18)<<endl;
cout<<12 * 18 / gcd(12, 18)<<endl;
// in built function for gcd
cout<<__gcd(12, 18)<<endl; //O(log(n))
// for three numbers gcd
// gcd(gcd(a,b),c)
//To find the minimum fraciton
/*
12 6 2
-- = --- = ---
18 9 3
so simple 12/GCD(12, 18) 12/6 2
---------------- = ------ = ---
18/GCD(12, 18) 18/6 3
*/
// also,
// gcd(a, b) == gcd(b, a%b) provided b is non zero otherwise a%b is not defined
cout<<gcd(12, 5)<<endl;
cout<<gcd(5, 2)<<endl;
return 0;
}