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:

Các kiểu dữ liệu trong java
OAuth2.0 and Dynamic Client Registration
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Spring Boot - Service Components
Configure a RestTemplate with RestTemplateBuilder
Spring Boot - Creating Docker Image
Java Program to Implement Jarvis Algorithm
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Perform the Unique Factorization of a Given Number
Hướng dẫn Java Design Pattern – Mediator
Serverless Functions with Spring Cloud Function
Registration with Spring Security – Password Encoding
Spring Security Registration – Resend Verification Email
Java Program to Implement Lloyd’s Algorithm
Tìm hiểu về Web Service
Removing all duplicates from a List in Java
Quick Guide on Loading Initial Data with Spring Boot
Generic Constructors in Java
How to Store Duplicate Keys in a Map in Java?
Spring Boot - Rest Template
The Guide to RestTemplate
Instance Profile Credentials using Spring Cloud
Java Program to Implement CountMinSketch
Getting Started with Forms in Spring MVC
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Find the Minimum value of Binary Search Tree
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Giới thiệu thư viện Apache Commons Chain
Filtering a Stream of Optionals in Java
Spring Webflux and CORS
Java Program to Implement Karatsuba Multiplication Algorithm