
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Stein’s Algorithm for finding GCD in C++
Stein's Algorithm used for discovering GCD of numbers as it calculates the best regular divisor of two non-negative whole numbers. It replaces division with math movements, examinations, and subtraction. In the event that both an and b are 0, gcd is zero gcd(0, 0) = 0. The algorithm for GCD(a,b) as follows;
Algorithm
START Step-1: check If both a and b are 0, gcd is zero gcd(0, 0) = 0. Step-2: then gcd(a, 0) = a and gcd(0, b) = b because everything divides 0. Step-3: check If a and b are both even, gcd(a, b) = 2*gcd(a/2, b/2) because 2 is a common divisor. Multiplication with 2 can be done with a bitwise shift operator. Step-4: If a is even and b is odd, gcd(a, b) = gcd(a/2, b). Similarly, if a is odd and b is even, then gcd(a, b) = gcd(a, b/2). It is because 2 is not a common divisor. Step-5: If both a and b are odd, then gcd(a, b) = gcd(|a-b|/2, b). Note that difference of two odd numbers is even Step-6: Repeat steps 3–5 until a = b, or until a = 0. END
In the view of above algorithm to calculates the GCD of 2 numbers, the following C++ code is write down as;
Example
#include <bits/stdc++.h> using namespace std; int funGCD(int x, int y){ if (x == 0) return y; if (y == 0) return x; int k; for (k = 0; ((x | y) && 1) == 0; ++k){ x >>= 1; y >>= 1; } while ((x > 1) == 0) x >>= 1; do { while ((y > 1) == 0) y >>= 1; if (x > y) swap(x, y); // Swap u and v. y = (y - x); } while (y != 0); return x << k; } int main(){ int a = 24, b = 18; printf("Calculated GCD of numbers (24,18) is= %d\n", funGCD(a, b)); return 0; }
Output
Finally, the GCD of two supplied number 24 and 18 is calculated in 6 by applying Stein's Algorithm as follows;
Calculated GCD of numbers (24,18) is= 6
- Related Articles
- Euclidean Algorithm for calculating GCD in JavaScript
- Finding gcd of two strings in JavaScript
- Tarjan's Algorithm for Strongly Connected Components
- C / C++ Program for Dijkstra's shortest path algorithm
- Berkeley's Algorithm
- Dijkstra's algorithm in Javascript
- Prim's algorithm in Javascript
- Kruskal's algorithm in Javascript
- Kruskal's Minimum Spanning Tree Algorithm-Greedy algorithm in C++
- Finding middlemost element(s) in JavaScript
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- Dekker's algorithm in Operating System
- Finding LCM of more than two (or array) numbers without using GCD in C++
- Find Weekday using Zeller's Algorithm
- C program to implement Euclid’ s algorithm

Advertisements