Algorithm learning, algorithm Learning Website
Euclidean Algorithm
The euclidean algorithm is also called the moving phase division. It is an algorithm used to calculate the maximum common divisor of two integers.
Of course, you can also find the minimum public multiple.
Algorithm Implementation
In fact, the algorithm is implemented by an integer.a bTwo, one digit for each requestr = a % b, And thenbPut inaTorPut inbLocation, recursive call.
Yesgcd(a, b) { return gcd(b, a%b); }It looks like this.
The end condition is whena%b == 0.
Maximum common approx.
//// Main. cpp // Euclidean /// Created by Alps on 15/3/28. // Copyright (c) 2015 chen. all rights reserved. // # include <iostream> using namespace std; int gcd (int a, int B) {if (a % B = 0) {return B;} return gcd (B, a % B);} int main (int argc, const char * argv []) {int a = 14, B = 18; printf ("% d \ n ", gcd (a, B); return 0 ;}
The above is the greatest common divisor. In fact, this can also be used to obtain the minimum public multiple.
Minimum Public multiple
The minimum public multiple isa bThe product divided by the maximum common divisor of the two, that is, their least common multiple. The Code is as follows:
int MinMultiple( int a, int b){ return (a * b)/gcd(a, b);}
In this way, you can.