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 Fermat Primality Test Algorithm
A Guide to Java HashMap
Java Program to Implement vector
How to Implement Caching using Adonis.js 5
Java 8 – Powerful Comparison with Lambdas
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
HashSet trong java
Java Program to Implement Red Black Tree
An Introduction to Java.util.Hashtable Class
Java Program to Implement D-ary-Heap
Spring MVC Custom Validation
Hướng dẫn Java Design Pattern – Adapter
Java Program to Implement ConcurrentSkipListMap API
Giới thiệu về Stream API trong Java 8
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement Segment Tree
Java Program to Represent Graph Using Incidence List
Read an Outlook MSG file
Default Password Encoder in Spring Security 5
Apache Commons Collections OrderedMap
A Guide to WatchService in Java NIO2
Convert String to int or Integer in Java
Guide to CopyOnWriteArrayList
Form Validation with AngularJS and Spring MVC
Java Program to Implement Leftist Heap
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Spring 5 Testing with @EnabledIf Annotation
Spring RestTemplate Error Handling
Java Program to Perform the Sorting Using Counting Sort
Class Loaders in Java