-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2609.java
54 lines (38 loc) · 939 Bytes
/
2609.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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int gcd1 = gcd_recursive(a,b);
int gcd2 = gcd_loop(a,b);
int lcm = (a*b) / gcd1;
if(gcd1 != gcd2)
System.exit(-1);
System.out.println(gcd1);
System.out.println(lcm);
}
public static int gcd_recursive(int a, int b) {
if(a<b) {
int temp = a;
a = b;
b = temp;
}
if(b==0)
return a;
return gcd_recursive(b, a%b);
}
public static int gcd_loop(int a, int b) {
if(a<b) {
int temp = a;
a = b;
b = temp;
}
while(b != 0) {
int temp = a%b;
a = b;
b = temp;
}
return a;
}
}