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 Expression Tree
Generating Random Numbers in a Range in Java
Converting Strings to Enums in Java
How to Kill a Java Thread
Java Program to Implement a Binary Search Tree using Linked Lists
Binary Numbers in Java
The Registration API becomes RESTful
Spring Data MongoDB Transactions
Partition a List in Java
Spring’s RequestBody and ResponseBody Annotations
Guide to PriorityBlockingQueue in Java
Java String to InputStream
Guide to java.util.Formatter
The Thread.join() Method in Java
Spring Boot - Building RESTful Web Services
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Autoboxing và Unboxing trong Java
Sending Emails with Java
Mapping a Dynamic JSON Object with Jackson
New Features in Java 13
Spring Cloud Bus
Check If a File or Directory Exists in Java
Request Method Not Supported (405) in Spring
Spring Boot Configuration with Jasypt
Encode a String to UTF-8 in Java
Concatenating Strings In Java
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Construct an Expression Tree for an Prefix Expression
Functional Interface trong Java 8
Từ khóa this và super trong Java
Queue và PriorityQueue trong Java
An Intro to Spring Cloud Zookeeper