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 – Generate Random String
Java Program to Implement Sparse Array
Java Program to Implement Direct Addressing Tables
Java Program to Implement the String Search Algorithm for Short Text Sizes
Check If Two Lists are Equal in Java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Find kth Largest Element in a Sequence
The Order of Tests in JUnit
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Java Program to Implement the RSA Algorithm
Java Program to Describe the Representation of Graph using Adjacency List
Working With Maps Using Streams
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Registration – Password Strength and Rules
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement Queue using Linked List
Java Program to Implement Binomial Heap
Hướng dẫn Java Design Pattern – MVC
Handling URL Encoded Form Data in Spring REST
Constructor Injection in Spring with Lombok
HttpClient Basic Authentication
Giới thiệu Google Guice – Dependency injection (DI) framework
Java 8 Streams peek() API
Spring Boot Configuration with Jasypt
Java – Reader to String
Java Program to Implement SimpeBindings API
Guide to Apache Commons CircularFifoQueue
A Guide to Java SynchronousQueue
Spring Boot - File Handling
HttpClient Connection Management
Spring Data JPA Delete and Relationships