-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEuclid.java
More file actions
44 lines (38 loc) · 956 Bytes
/
Euclid.java
File metadata and controls
44 lines (38 loc) · 956 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.Scanner;
public class Euclid {
static class triplet{
int x,y,gcd;
}
public static triplet egcd(int a,int b)
{ //ax+by=gcd(a,b)
if(b==0)
{
triplet ans=new triplet();
ans.x=1;
ans.y=0;
ans.gcd=a;
return ans;
}
triplet smallAns=egcd(b,a%b);
triplet ans=new triplet();
ans.x=smallAns.y;
ans.gcd=smallAns.gcd;
ans.y=smallAns.x-(a/b)*smallAns.y;
return ans;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
triplet ans=egcd(a, b);
System.out.println("x\ty\tgcd");
System.out.println(ans.x+"\t"+ans.y+"\t"+ans.gcd);
sc.close();
}
}