Java Program to Implement Extended Euclid Algorithm

This is a Java Program to Implement Extended Euclid Algorithm. The extended Euclidean algorithm is an extension to the Euclidean algorithm. Besides finding the greatest common divisor of integers a and b, as the Euclidean algorithm does, it also finds integers x and y (one of which is typically negative) that satisfy Bézout’s identity
ax + by = gcd(a, b).

Here is the source code of the Java Program to Implement Extended Euclid Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to implement Extended Euclid  Algorithm
 **/
 
import java.util.Scanner;
 
/** Class ExtendedEuclid **/
public class ExtendedEuclid
{
    /** Function to solve **/
    public void solve(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("Roots  x : "+ lastx +" y :"+ lasty);
    }
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Extended Euclid Algorithm Test\n");
        /** Make an object of ExtendedEuclid class **/
        ExtendedEuclid ee = new ExtendedEuclid();
 
        /** Accept two integers **/
        System.out.println("Enter a b of ax + by = gcd(a, b)\n");
        long a = scan.nextLong();
        long b = scan.nextLong();
        /** Call function solve of class ExtendedEuclid **/
        ee.solve(a, b);        
    }
}

Output:

Extended Euclid Algorithm Test
 
Enter a b of ax + by = gcd(a, b)
 
120 23
Roots  x : -9 y :47

Related posts:

Arrays.asList vs new ArrayList(Arrays.asList())
Date Time trong Java 8
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Toán tử trong java
Wrapper Classes in Java
Template Engines for Spring
Spring @RequestParam Annotation
How to Get a Name of a Method Being Executed?
Derived Query Methods in Spring Data JPA Repositories
How to Read a Large File Efficiently with Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Convert Hex to ASCII in Java
How to Remove the Last Character of a String?
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Giới thiệu Google Guice – Dependency injection (DI) framework
Java Program to Implement Merge Sort Algorithm on Linked List
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement the MD5 Algorithm
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Hướng dẫn Java Design Pattern – Adapter
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Send email with SMTPS (eg. Google GMail)
MyBatis with Spring
Java Program to Perform the Unique Factorization of a Given Number
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Converting Between Byte Arrays and Hexadecimal Strings in Java
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Tips for dealing with HTTP-related problems
Hướng dẫn Java Design Pattern – Visitor
Difference Between Wait and Sleep in Java
Java Program to Implement Min Hash
Java Program to Perform Polygon Containment Test