Java Program for Extended Euclidean algorithms
Last Updated :
20 Feb, 2023
GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors.
Java
import java.util.*;
import java.lang.*;
class GFG {
static public void gcdExtended( long a, long b)
{
long x = 0 , y = 1 , lastx = 1 , lasty = 0 , temp;
while (b != 0 )
{
long q = a / b;
long 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( "GCD " +a+ " and its Roots x : " + lastx + " y :" + lasty);
}
public static void main(String[] args)
{
long a = 35 , b = 15 ;
gcdExtended(a, b);
}
}
|
Please refer complete article on Basic and Extended Euclidean algorithms for more details!
Share your thoughts in the comments
Please Login to comment...