forked from Nehanshj/Algorithmic-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCD.java
More file actions
34 lines (29 loc) · 664 Bytes
/
Copy pathGCD.java
File metadata and controls
34 lines (29 loc) · 664 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
import java.util.*;
public class GCD {
/* NAIVE GCD ALGO
private static int gcd_naive(int a, int b) {
int current_gcd = 1;
for(int d = 2; d <= a && d <= b; ++d) {
if (a % d == 0 && b % d == 0) {
if (d > current_gcd) {
current_gcd = d;
}
}
}
return current_gcd;
}
*/
public static int euclid_gcd(int a, int b){
if(b==0){
return a;
}
int x = a%b;
return euclid_gcd(b,x);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(euclid_gcd(a, b));
}
}