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:
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Simple Single Sign-On with Spring Security OAuth2
Sending Emails with Java
Java Program to Implement Sorted Vector
Java Program to Implement Skew Heap
Jackson – Decide What Fields Get Serialized/Deserialized
ExecutorService – Waiting for Threads to Finish
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Spring Web Annotations
Testing in Spring Boot
Guide to the ConcurrentSkipListMap
Java Program to Implement the Hill Cypher
Simultaneous Spring WebClient Calls
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Describe the Representation of Graph using Incidence List
Java TreeMap vs HashMap
Guide to Apache Commons CircularFifoQueue
Base64 encoding và decoding trong Java 8
Introduction to Eclipse Collections
Java Program to Represent Linear Equations in Matrix Form
Java Program to Compute Cross Product of Two Vectors
Getting a File’s Mime Type in Java
A Guide to TreeSet in Java
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Tips for dealing with HTTP-related problems
A Guide to BitSet in Java
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Serve Static Resources with Spring
Hướng dẫn Java Design Pattern – Builder
Connect through a Proxy
Copy a List to Another List in Java