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:
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Show Hibernate/JPA SQL Statements from Spring Boot
Hướng dẫn Java Design Pattern – Factory Method
Test a REST API with Java
Java Program to Implement Solovay Strassen Primality Test Algorithm
Spring Web Annotations
Tiêu chuẩn coding trong Java (Coding Standards)
Spring Security – security none, filters none, access permitAll
Introduction to Project Reactor Bus
Jackson vs Gson
Sorting in Java
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Abstract class và Interface trong Java
Vector trong Java
HttpClient Basic Authentication
Comparing Long Values in Java
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Java Timer
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Create a Random Linear Extension for a DAG
Java Program to Implement Lloyd’s Algorithm
Guide to Java Instrumentation
Spring MVC Async vs Spring WebFlux
Java Program to Perform Cryptography Using Transposition Technique
Using the Not Operator in If Conditions in Java
Java Program to Perform Searching Based on Locality of Reference
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Spring Boot - Scheduling
Spring Security Registration – Resend Verification Email
Java Program to Implement Hopcroft Algorithm
Java Program to Describe the Representation of Graph using Incidence List
Add Multiple Items to an Java ArrayList