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:
Java Program to Implement vector
ClassNotFoundException vs NoClassDefFoundError
Java Program to Perform Deletion in a BST
Từ khóa static và final trong java
Java Program to Implement Affine Cipher
wait() and notify() Methods in Java
Java Program to Find All Pairs Shortest Path
Set Interface trong Java
Java CyclicBarrier vs CountDownLatch
Java Program to Perform Left Rotation on a Binary Search Tree
Mockito and JUnit 5 – Using ExtendWith
Guide to @JsonFormat in Jackson
Sorting Query Results with Spring Data
Logout in an OAuth Secured Application
Một số ký tự đặc biệt trong Java
Posting with HttpClient
Fixing 401s with CORS Preflights and Spring Security
Java 9 Stream API Improvements
How to Change the Default Port in Spring Boot
Java Program to Implement Flood Fill Algorithm
Java Program to Implement Sorted Doubly Linked List
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Implement VList
Jackson – Bidirectional Relationships
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Hướng dẫn Java Design Pattern – Proxy
The StackOverflowError in Java
Apache Commons Collections SetUtils
Java Program to Perform String Matching Using String Library
A Guide to JUnit 5 Extensions
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Perform Search in a BST