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 Comparison Between Spring and Spring Boot
Hướng dẫn Java Design Pattern – Chain of Responsibility
Configure a Spring Boot Web Application
Custom JUnit 4 Test Runners
Java Program to Implement Gabow Algorithm
Java Program to Implement Fibonacci Heap
Java Program to Implement SimpeBindings API
Một số ký tự đặc biệt trong Java
Logout in an OAuth Secured Application
Spring Boot - Twilio
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Adding Shutdown Hooks for JVM Applications
A Guide to JPA with Spring
Java Program to Implement Ternary Heap
Spring Boot - Quick Start
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java – Reader to Byte Array
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Date Time trong Java 8
Java Program to Show the Duality Transformation of Line and Point
Spring Boot - Google Cloud Platform
Lập trình đa luồng trong Java (Java Multi-threading)
Constructor Injection in Spring with Lombok
Converting Between Byte Arrays and Hexadecimal Strings in Java
Removing all Nulls from a List in Java
A Guide to Java SynchronousQueue
ETags for REST with Spring
Java Program to Implement LinkedList API
Java Program to Perform Searching Based on Locality of Reference
The Guide to RestTemplate