# Binary algorithm for calculating the NODE.

The binary algorithm for calculating the NOD, as the name implies, finds the largest common divisor, namely the NOD of two integers. In efficiency, this algorithm is superior to the Euclid method, which is associated with the use of shifts, that is, the operations of dividing by the degree of 2, in our case by 2.

It is easier for a computer to divide (multiply) by 2, 4, 8, etc., than by any other number. But at the same time, the binary algorithm is inferior to euclid's algorithm in simplicity of implementation. For further assimilation of the material, you should familiarize yourself with the properties possessed by the NOD of the two numbers A and B. Not all properties will be required, but only the following three identities:

**NOD(2**

*A*, 2*B*) = 2NOD(*A*,*B*)**NOD(2**

*A*, 2*B*+1) = NODE(*A*, 2*B*+1)**RHODE(-**

*A*,*B*) = RHAD(*A*,*B*)**Now let's consider the stages of the algorithm. They are based on the given properties of the largest common divisor.**

- initialize the variable
*k*with the value 1. Its task is to calculate the "disproportionality" obtained as a result of division. While*A*and*B*are halved, it will double; - as long as
*A*and*B*are not simultaneously zero, we execute- if
*A*and*B*are even numbers, then divide each of them in two:*A*←*A*/2,*B*←*B*/2, and double*k*:*k*←*k**2, until at least one of the numbers*A*or*B*becomes odd; - if
*A*is even and*B*is odd, then we divisible*A*as long as division without residue is possible; - if
*B*is even and*A*is odd, then we divide*B*as long as division is possible without residue; - if
*A*≥*B*, then replace*A*with the difference*A*and*B*, otherwise B will replace*B*with the difference*B*and*A*.

- if
- after exiting the 2nd point, it remains to return as a result the product
*B*and*k*: NOD(*A*,*B*) =*B***k*.

### The code of the program in C++:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include "stdafx.h" #include <iostream> using namespace std; //binary algorithm for calculating GCD int NOD(int A, int B) { intk=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"); } |

### The code of the program in Pascal:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | program BinaryNOD; usescrt; var A, B: integer; {binary algorithm for computing gcd} function NOD(A, B: integer): integer; vark: 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:=Adiv 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:=A-B else B:=B-A; end; NOD:=B*k; end; {main program block} begin write('A > '); read(A); write('B > '); read(B); write('NOD(', A, ', ', B, '): ', NOD(A, B)); end. |

An interesting fact is that the algorithm was known in China of the 1st century AD, but the year of its publication was only 1967, when the Israeli programmer and physicist Joseph Stein published the algorithm. In view of this, there is an alternative name for the method - the Stein algorithm.