This is a Java Program to Implement Stein GCD Algorithm. The binary GCD algorithm, also known as Stein’s algorithm, is an algorithm that computes the greatest common divisor of two nonnegative integers. Stein’s algorithm uses simpler arithmetic operations than the conventional Euclidean algorithm. It replaces division with arithmetic shifts, comparisons and subtraction.
Here is the source code of the Java Program to Implement Stein GCD Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to Implement Stein GCD Algorithm
**/
import java.util.Scanner;
/** Class SteinGcd **/
public class SteinGcd
{
/** Function to calculate gcd **/
public int gcd(int u, int v)
{
int shift;
if (u == 0)
return v;
if (v == 0)
return u;
for (shift = 0; ((u | v) & 1) == 0; ++shift)
{
u >>= 1;
v >>= 1;
}
while ((u & 1) == 0)
u >>= 1;
do
{
while ((v & 1) == 0)
v >>= 1;
if (u > v)
{
int t = v;
v = u;
u = t;
}
v = v - u;
} while (v != 0);
return u << shift;
}
/** Main function **/
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Stein GCD Algorithm Test\n");
/** Make an object of SteingGcd class **/
SteinGcd sg = new SteinGcd();
/** Accept two integers **/
System.out.println("Enter two integer numbers\n");
int n1 = scan.nextInt();
int n2 = scan.nextInt();
/** Call function gcd of class SteinGcd **/
int gcd = sg.gcd(n1, n2);
System.out.println("GCD of "+ n1 +" and "+ n2 +" = "+ gcd);
}
}
Output:
Stein GCD Algorithm Test Enter two integer numbers 32984 10013 GCD of 32984 and 10013 = 589
Related posts:
Guide to CopyOnWriteArrayList
Check if there is mail waiting
Generating Random Dates in Java
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Biến trong java
Bootstrap a Web Application with Spring 5
Java Program to Perform the Unique Factorization of a Given Number
Vòng lặp for, while, do-while trong Java
Lấy ngày giờ hiện tại trong Java
Java Program to Implement HashSet API
Object Type Casting in Java
Java Program to Implement the Hill Cypher
Period and Duration in Java
Java – String to Reader
Java Program to Implement Max Heap
Remove All Occurrences of a Specific Value from a List
Debug a JavaMail Program
Spring AMQP in Reactive Applications
Spring Data Reactive Repositories with MongoDB
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to do a Depth First Search/Traversal on a graph non-recursively
SOAP Web service: Authentication trong JAX-WS
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Spring Boot - File Handling
Apache Commons Collections MapUtils
Logout in an OAuth Secured Application
Shuffling Collections In Java
Initialize a HashMap in Java
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Implement Floyd Cycle Algorithm
Receive email using POP3