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 Hash Tree
Java Program to Implement LinkedHashSet API
Kết hợp Java Reflection và Java Annotations
Java Program to Implement Max Heap
RegEx for matching Date Pattern in Java
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Convert Hex to ASCII in Java
Java Program to Implement Miller Rabin Primality Test Algorithm
Spring Security OAuth Login with WebFlux
Introduction to Spring Method Security
Java Program to Perform Searching Based on Locality of Reference
Toán tử instanceof trong java
Tạo số và chuỗi ngẫu nhiên trong Java
Introduction to Spring Data JDBC
Converting String to Stream of chars
Getting Started with Forms in Spring MVC
Java String Conversions
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Implement Ternary Search Algorithm
Java InputStream to String
Lập trình đa luồng với CompletableFuture trong Java 8
Beans and Dependency Injection
Biểu thức Lambda trong Java 8 – Lambda Expressions
A Guide to BitSet in Java
Converting Between an Array and a Set in Java
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java Program to Implement Borwein Algorithm
Java Program to Implement D-ary-Heap
Spring RestTemplate Error Handling
Java Program to Implement Find all Forward Edges in a Graph
Guide to the Synchronized Keyword in Java
Examine the internal DNS cache