-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCDAndLCM.java
61 lines (52 loc) · 1.5 KB
/
GCDAndLCM.java
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
package math.easy;
/***
* Given two numbers A and B. The task is to find out their LCM and GCD.
*
* Example 1:
* Input: A = 5, B = 10
* Output: 10 5
*
* Example 2:
* Input: A = 14, B = 8
* Output: 56 2
*/
public class GCDAndLCM {
public static void main(String[] args) {
int a = 14, b = 8;
System.out.println("LCM is: " + findLCMOf(a, b));
System.out.println("GCD using Brute Force is: " + findGCDUsingBruteForce(a, b));
System.out.println("GCD using Euclidean Subtraction is: " + findGCDUsingEuclideanSubtraction(a, b));
System.out.println("GCD using Euclidean Division is: " + findGCDUsingEuclideanDivision(a, b));
}
private static int findGCDUsingBruteForce(int a, int b) {
int smaller = Math.min(a, b);
for (int i = smaller; i >= 1; i--) {
if ((a % i == 0) && (b % i == 0)) {
return i;
}
}
return -1;
}
private static int findGCDUsingEuclideanSubtraction(int a, int b) {
while (a != b) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return a;
}
private static int findGCDUsingEuclideanDivision(int a, int b) {
while (b != 0) {
int temp = a;
a = b;
b = temp % b;
}
return a;
}
private static int findLCMOf(int a, int b) {
int gcd = findGCDUsingEuclideanDivision(a, b);
return (a * b) / gcd;
}
}