Lecture
The binary algorithm for computing GCD, as the name implies, finds the greatest common divisor, namely GCD of two integers. In efficiency, this algorithm is superior to the Euclidean method, which is associated with the use of shifts, that is, division by a power of 2, in our case by 2. It is easier to divide (multiply) by 2, 4, 8, etc., than any other number. But at the same time, the binary algorithm is inferior to the Euclidean algorithm in the simplicity of implementation. For further assimilation of the material, one should become familiar with the properties that the GCD has of two numbers A and B. One does not need all the properties, but only the following three identities:
Now consider the stages of the algorithm. They are based on the properties of the greatest common divisor.
Here you will have to acquire a variable that will calculate the "disproportion" resulting from the division. Call it k and equalize 1; while A and B are halved, it should be doubled (k ← k * 2).
In the program listing, the actions described above will be carried out using the following instructions. The first numbered item corresponds to a common outer cycle, which is executed under the condition that the numbers A and B are not equal to zero at the same time. It contains three independent cycles (1, 2 and 3 marked points), as well as a conditional operator in full form (clause 4). Numbered item 2 corresponds to the usual return of the resulting value, i.e. the desired GCD.
one 2 3 four five 6 7 eight 9 ten eleven 12 13 14 15 sixteen 17 18 nineteen 20 21 22 23 24 25 26 27 28 29 thirty 31 | #include "stdafx.h" #include <iostream> using namespace std; // binary algorithm for calculating gcd int NOD (int A, int B) { int k = 1; while ((A! = 0) && (B! = 0)) { while ((A% 2 == 0) && (B% 2 == 0)) { A / = 2; B / = 2; k * = 2; } while (A% 2 == 0) A / = 2; while (B% 2 == 0) B / = 2; if (A> = B) A- = B; else B- = A; } return B * k; } // main function void main () { setlocale (LC_ALL, "Rus"); int A, B; cout << "A>"; cin >> A; cout << "B>"; cin >> B; cout << "NOD (" << A << "," << B << ") =" << NOD (A, B); system ("pause >> void"); } |
one 2 3 four five 6 7 eight 9 ten eleven 12 13 14 15 sixteen 17 18 nineteen 20 21 22 23 24 25 26 27 28 | program BinaryNOD; uses crt; var A, B: integer; {binary algorithm for calculating gcd} function NOD (A, B: integer): integer; var k: integer; begin k: = 1; while (a <> 0) and (b <> 0) do begin while (A mod 2 = 0) and (B mod 2 = 0) do begin A: = A div 2; B: = B div 2; k: = k * 2; end; while A mod 2 = 0 do A: = A div 2; while B mod 2 = 0 do B: = B div 2; if A> = B then A: = AB else B: = BA; end; NOD: = B * k; end; {main program block} begin write ('A>'); read (A); write ('b>'); read (B); write ('GCD (', A, ',', B, '):', NOD (A, B)); end. |
An interesting fact is that the algorithm was already known in China of the 1st century AD. e., but the year of its publication was only 1967, when an Israeli programmer and physicist Joseph Stein published an algorithm. In view of this, there is an alternative name for the method, the Stein algorithm.
Comments
To leave a comment
Algorithms
Terms: Algorithms