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:
A Quick Guide to Spring Cloud Consul
Java Program to find the peak element of an array using Binary Search approach
Phương thức tham chiếu trong Java 8 – Method References
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Predicate trong Java 8
Spring Webflux and CORS
New Features in Java 9
Spring Boot - Logging
Java Program to Implement Sorted Doubly Linked List
Tính trừu tượng (Abstraction) trong Java
Java Program to Implement CopyOnWriteArrayList API
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Implement Network Flow Problem
Quick Guide to @RestClientTest in Spring Boot
Adding Shutdown Hooks for JVM Applications
Java Program to Implement LinkedBlockingDeque API
Spring Boot - Bootstrapping
Java Program to Implement Weight Balanced Tree
Java Program to Perform Sorting Using B-Tree
Java Program to Compare Binary and Sequential Search
Removing all Nulls from a List in Java
Spring MVC Tutorial
Convert XML to JSON Using Jackson
Java Program to Perform Partition of an Integer in All Possible Ways
Transactions with Spring and JPA
Java Program to Represent Graph Using Linked List
Serialization và Deserialization trong java
Rate Limiting in Spring Cloud Netflix Zuul
HttpAsyncClient Tutorial
Spring Boot - Web Socket
Array to String Conversions
New in Spring Security OAuth2 – Verify Claims