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:
Upload and Display Excel Files with Spring MVC
Java Program to Create the Prufer Code for a Tree
String Processing with Apache Commons Lang 3
Getting Started with GraphQL and Spring Boot
Java Program to Implement the Checksum Method for Small String Messages and Detect
Giới thiệu Aspect Oriented Programming (AOP)
Removing all Nulls from a List in Java
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Logging in Spring Boot
Remove HTML tags from a file to extract only the TEXT
Hashtable trong java
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement Skew Heap
Introduction to Spring Security Expressions
Join and Split Arrays and Collections in Java
Simple Single Sign-On with Spring Security OAuth2
Java Program for Topological Sorting in Graphs
Java Program to Implement Tarjan Algorithm
Getting a File’s Mime Type in Java
Sorting in Java
Guava – Join and Split Collections
Hướng dẫn Java Design Pattern – DAO
Tính kế thừa (Inheritance) trong java
Introduction to Spring Cloud Netflix – Eureka
A Quick JUnit vs TestNG Comparison
Introduction to Spring Cloud OpenFeign
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Check If Two Lists are Equal in Java
Receive email by java client
Java Program to Implement Affine Cipher
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Implement Stack using Linked List