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:
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Jackson – Decide What Fields Get Serialized/Deserialized
Spring Boot: Customize the Jackson ObjectMapper
The Registration API becomes RESTful
Hướng dẫn Java Design Pattern – Template Method
Java Program to Show the Duality Transformation of Line and Point
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Introduction to Spring Data REST
Spring WebFlux Filters
Spring Cloud – Securing Services
Java Program to Implement Ternary Heap
New Features in Java 12
Automatic Property Expansion with Spring Boot
Java Program to Implement Adjacency List
Hướng dẫn sử dụng String Format trong Java
Assertions in JUnit 4 and JUnit 5
A Guide to TreeMap in Java
How to Get a Name of a Method Being Executed?
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Spring JDBC
Java Program to Implement Range Tree
Class Loaders in Java
Converting Iterator to List
Jackson – Marshall String to JsonNode
Java Program to Implement Hash Tables Chaining with Binary Trees
Spring Data – CrudRepository save() Method
Spring WebClient and OAuth2 Support
Java Program to Implement Interval Tree
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Dynamic Proxies in Java
Java Streams vs Vavr Streams