-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExEuclidean.java
70 lines (47 loc) · 1.19 KB
/
ExEuclidean.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
package euclid;
import java.util.Scanner;
public class ExEuclidean {
/** Function to ExEuFormula **/
public int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public void ExEuFormula(int a, int b)
{
int x = 0, y = 1, lastx = 1, lasty = 0, temp;
while (b != 0)
{
int q = a / b;
int r = a % b;
a = b;
b = r;
temp = x;
x = lastx - q * x;
lastx = temp;
temp = y;
y = lasty - q * y;
lasty = temp;
}
System.out.println("Roots x : " + lastx + " y :" + lasty);
}
/** Main function **/
public static void main(String[] args)
{
int gcd;
Scanner scan = new Scanner(System.in);
System.out.println("Extended Euclidean Alogorithm\n");
/** Object instantiation ExEuclidean class **/
ExEuclidean ee = new ExEuclidean();
/** Input two integers **/
System.out.println("Enter a b of ax + by = gcd(a, b)\n");
int a = scan.nextInt();
int b = scan.nextInt();
/** Call function ExEuFormula of class ExEuclidean **/
gcd = ee.gcd(a, b);
System.out.println("Using Euclid's algorithm we determine that gcd of a=" + a + " and b=" + b +" is: " + gcd + "\n" );
ee.ExEuFormula(a, b);
scan.close();
}
}