Tossing the mission and shift subtraction (Euclid & Stein algorithm)
Give the stein algorithm as follows:
If A = 0, B is the maximum number of conventions, the algorithm ends if b = 0, A is the maximum number of conventions, the algorithm ends set A1 = A, B1 = B and C1 = 1 If the AN and BN are even, then AN 1 = A / 2, BN 1 = Bn / 2, CN 1 = CN * 2 (note, multiply 2 as long as the integer is shifted one bit, except for the right to right to right) if an AN is Agile, BN is not even, then AN 1 = A / 2, BN 1 = BN, CN 1 = CN (very obvious, 2 is not odd) If BN is even, AN is not even, then Bn 1 = BN / 2, AN 1 = A, CN 1 = CN (very clear, 2 is not odd) If both and bn are not even, then AN 1 = | an -Bn |, BN 1 = min (an, bn), CN 1 = CN N , turn 4
// Greatest Common Divisor // By Heaton // 2005/03/11
#include
Using namespace std;
// Exchange A, B VOID SWAP (INT & A1, INT & B1) {INT TEMP; TEMP = A1; A1 = B1; B1 = Temp;} // Rolling Fraction Int GCD (INT A, INT B) {IF (a
// Shift subtraction INT GCD2 (int A, int b) {IF (a, b) swap (a, b); // cout << a << "<< b << endl; // Track A , B value IF (a == 0) RETURN B; if (b == 0) Return A; While (A! = B) {IF (((A & 1) == 0) && ((B & 1) == 0 )) {RETURN 2 * GCD2 (A / 2, B / 2); // A, B Are Even Numbers} else IF (((A & 1) == 1) && ((B & 1) == 0)) {Return GCD2 (a, b / 2); // a is odd number, b is even number} else IF ((A & 1) == 0) && ((B & 1) == 1)) {RETURN GCD2 (A / 2, B ); // a .... Even; b ... odd ...} else IF ((A & 1) == 1) && ((B & 1) == 1)) {Return GCD2 (B, (AB) ); // a, b is odd number}} RETURN B;} // By recursive call, N number, INT NGCD (INT A [= 1) {IF (n == 1) Return A [0]; ELSE RETURN GCD2 (A [N-1], NGCD (A, N-1));}
INT main () {Int m = GCD (15, 25); cout << m << "/ n shift phase subtraction / na b / n"; int N = GCD2 (15, 20); cout << n < <"/ n The Common Divisor of Array {450, 90, 15, 45} / n"; int A [] = {450, 90, 15, 45}; // should be sorted by ascending alignment to array INT x = NGCD (a, 4); cout << "value:" << x << Endl; /// ?????? Return 0;}