Previously only knew Euclid to divide the method, today learned another, in the processing of large number of more excellent Algorithm--stein
Hereby record
1. Euclidean (Euclid) algorithm
Also called the GCD method, according to the theorem of =gcd (a, b) (b,a%b)
Implementation process Demo: SAMPLE:GCD (15,10) =gcd (10,5) =gcd (5,0) =5
C language Implementation:
1 int EUCLID_GCD (intint b)2{3 return b? EUCLID_GCD (b, a%b): a; 4 }
2.Stein algorithm
In general, integers in real-world applications rarely exceed 64 bits (which of course now allow 128-bit), and for such integers, it is easy to calculate the modulo between two numbers. For a platform with a word length of 32 bits, the modulo of two integers with no more than 32 bits is required, only one instruction period is needed, and the integer modulus of 64 bits or less is calculated, but only a few cycles. But for larger prime numbers, such a process has to be designed by the user, in order to calculate two more than 64-bit integer modulo, the user may have to use similar to the Multi-digit division hand calculation process of the trial law, the process is not only complex, and consumes a lot of CPU Time. For modern cryptographic algorithms, the need to calculate more than 128 prime numbers abound, the design of such a program is eager to abandon division and Modulo.
Basis theorem:
GCD (a,a) =a, that is, a number and its own number of conventions is still its own. GCD (ka,kb) =k*gcd (a, b), that is, the greatest common divisor operation and the multiplier operation can be exchanged. In particular, when k=2, two even-numbered greatest common divisor must be divisible by 2. When K and B are mutually prime, gcd (ka,b) =gcd (a, a, b), that is, only one of the two numbers has no effect on the greatest common Divisor. In particular,when k=2, The description calculates an even and an odd number of greatest common divisor , you can divide the even number by 2。 C language Implementation:
1 intSTEIN_GCD (intXintY)2 {3 if(x = =0)returny;4 if(y = =0)returnx;5 if(x%2==0&& y%2==0)6 return 2* STEIN_GCD (x >>1, y >>1);7 Else if(x%2==0)8 returnSTEIN_GCD (x >>1, y);9 Else if(y%2==0)Ten returnSTEIN_GCD (x, y >>1); one Else a returnSTEIN_GCD (min (x, y), fabs (x-y)); -}
Two algorithms for finding greatest common divisor (GCD)