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:
Spring Boot - Quick Start
Serve Static Resources with Spring
Spring Cloud – Securing Services
Spring Boot - Web Socket
Java Program to Implement Bit Array
How to Find an Element in a List with Java
Form Validation with AngularJS and Spring MVC
Deque và ArrayDeque trong Java
Map to String Conversion in Java
Spring’s RequestBody and ResponseBody Annotations
Guide to @JsonFormat in Jackson
A Guide to the Java LinkedList
Debug a JavaMail Program
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Convert a Map to an Array, List or Set in Java
Spring Boot - Google Cloud Platform
Constructor Injection in Spring with Lombok
OAuth2.0 and Dynamic Client Registration
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Changing Annotation Parameters At Runtime
Exception Handling in Java
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Perform Sorting Using B-Tree
Java Program to Implement Interval Tree
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Hướng dẫn sử dụng Java Annotation
Java Program to Implement Variable length array
Why String is Immutable in Java?
Java – String to Reader
Hướng dẫn Java Design Pattern – Mediator
Java Program to Implement ArrayDeque API
Hướng dẫn Java Design Pattern – Service Locator