-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCM.java
86 lines (71 loc) · 1.6 KB
/
LCM.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
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
import java.math.BigDecimal;
import java.util.Scanner;
public class LCM {
private static long lcm_naive(int a, int b) {
for (long l = 1; l <= (long) a * b; ++l)
if (l % a == 0 && l % b == 0)
return l;
return (long) a * b;
}
private static long lcm_efficient(int a, int b) {
if(a==0||b==0) {
return 0;
}
if(a==1)
return b;
if(b==1)
return a;
if(b==a)
return a;
long product = (long) a*b;
long gcd = gcd_efficient(a, b);
//System.out.println(product);
//System.out.println(gcd);
return product/gcd;
}
private static long gcd_efficient(long a, long b) {
if(a==b) {
return a;
}
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}
if(a==1||b==1) {
return 1;
}
long bigger_num = a > b ? a : b;
long smaller_num = a > b ? b : a;
long rem = bigger_num % smaller_num;
long a1 = smaller_num;
long b1 = rem;
while (rem > 0) {
rem = a1 % b1;
a1 = b1;
b1= rem==0?b1:rem;
}
return b1;
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
//System.out.println(lcm_naive(a, b));
System.out.println(lcm_efficient(a, b));
//Stress testing code
/*while(true) {
int a = new Random().nextInt(10000);
int b = new Random().nextInt(10000);
long rs1 = lcm_naive(a,b);
long rs2 = lcm_efficient(a,b);
if(rs1!=rs2) {
System.out.println("Failure");
break;
}else {
System.out.println("OK");
}
}*/
}
}