-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCDExtended.java
More file actions
44 lines (42 loc) · 891 Bytes
/
GCDExtended.java
File metadata and controls
44 lines (42 loc) · 891 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
35
36
37
38
39
40
41
42
43
44
import java.util.*;
public class GCDExtended {
static class Coefficient{
int x,y;
Coefficient(int x,int y){
this.x=x;
this.y=y;
}
}
static Coefficient coeff=null;
public static int ExtendedEuclidean(int a,int b) {
if(a==0) {
coeff=new Coefficient(0,1);
return b;
}
else {
int gcd=ExtendedEuclidean(b%a,a);
int x,y;
x=coeff.y-Math.floorDiv(b, a)*coeff.x;
y=coeff.x;
coeff=new Coefficient(x,y);
return gcd;
}
}
public static int euclidean(int a,int b) {
if(a==0) {
return b;
}
else {
return euclidean(b%a,a);
}
}
public static void main(String[] args) {
int a,b;
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
System.out.println("GCD("+a+","+b+") :"+ExtendedEuclidean(a,b));
System.out.println(a+"*"+coeff.x+" + "+b+"*"+coeff.y);
sc.close();
}
}