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:
Intro to the Jackson ObjectMapper
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Find Inverse of a Matrix
ClassNotFoundException vs NoClassDefFoundError
Java Stream Filter with Lambda Expression
Mệnh đề if-else trong java
Properties with Spring and Spring Boot
Java 8 Streams peek() API
Java Program to Implement Suffix Array
Java Program to Find a Good Feedback Vertex Set
Java Program to find the peak element of an array using Binary Search approach
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Solve Knapsack Problem Using Dynamic Programming
Comparing Arrays in Java
Java Deep Learning Essentials - Yusuke Sugomori
Spring Security – Reset Your Password
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Guide to java.util.concurrent.Locks
Hướng dẫn sử dụng lớp Console trong java
Spring Data JPA and Null Parameters
Một số ký tự đặc biệt trong Java
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Getting the Size of an Iterable in Java
An Intro to Spring Cloud Contract
Guide to Apache Commons CircularFifoQueue
Configuring a DataSource Programmatically in Spring Boot
Summing Numbers with Java Streams
Spring Boot - Tomcat Deployment
String Processing with Apache Commons Lang 3
Assertions in JUnit 4 and JUnit 5