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 Check whether Undirected Graph is Connected using DFS
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Uploading MultipartFile with Spring RestTemplate
Rate Limiting in Spring Cloud Netflix Zuul
How to Convert List to Map in Java
Spring 5 and Servlet 4 – The PushBuilder
Hashing a Password in Java
Cơ chế Upcasting và Downcasting trong java
Đồng bộ hóa các luồng trong Java
Giới thiệu SOAP UI và thực hiện test Web Service
Introduction to Spring Data JPA
Adding a Newline Character to a String in Java
Java Program to Solve Tower of Hanoi Problem using Stacks
Multi Dimensional ArrayList in Java
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Guide to Java 8’s Collectors
Spring Security with Maven
New Features in Java 12
Spring Boot - Exception Handling
Từ khóa static và final trong java
Tìm hiểu về Web Service
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Using Custom Banners in Spring Boot
Flattening Nested Collections in Java
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Describe the Representation of Graph using Incidence Matrix
Guide to the Java Queue Interface
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Guide to Character Encoding
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree