Algorithm learning, algorithm Learning Website

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.