The greatest common divisor of algorithm hand-over operation

Source: Internet
Author: User
Tags gcd greatest common divisor

Write today the greatest common divisor algorithm, the English abbreviation for the GCD algorithm.

1. Recursive solution:
/*书上并没有给出递归的解法,可能是觉得这个解法不是很完美,但是给出来就当学习下递归。*/int Gcd(unsignedunsigned num2){    if00)//算法基于欧几里德的算法。        return (num1 > num2) ? num1 : num2;    return Gcd(num2, num1%num2);}//代码写的很短小,但是效率可能不是很高//欧几里德算法有一个结论,两个整数A,B.如果A,B有一个为0,那么另一个就是最大公约数。其余的内容可以谷歌。
2.for Cyclic Solution:
//This place is forced to use the c++11 characteristics, but may not be very good. //Go back to the book and try to use the C++11 feature to re-optimize the wording. int&& GCD (Const unsigned&AMP;NUM1,Const unsigned&AMP;NUM2) {if(NUM1 = =0|| Num2 = =0)return STD:: Move ((Num1 > Num2)? num1:num2);intGCD =0; for(inti =1; I <int(NUM1); ++i) {if(num1%i = =0) && (num2%i = =0)) GCD = i;Else            Continue; }return STD:: Move (GCD);}//This place is also very ingenious, belonging to the normal idea. Since we are asking for the greatest common divisor, then we can start from 1, greatest common divisor is certainly less than or equal to any given two integers, we only need to use the loop starting from 1 to try, save to meet (num1%i = = 0) && (num2%i = = 0) Condition of the maximum I can. 
3.while Cyclic Solution:
int Gcd(unsignedunsigned num2){if00)return//上面一行和本行不是必须的。    while0){        int rem = num1%num2;        num1 = num2;        num2 = rem;    }    return num1;//原理和上面类似,递归可以和循环互改。
Optimization scenarios:

The optimization of this place is mainly the optimization of the parameters, and I do not have a better solution to the algorithm itself.

1.0
int Gcd(constunsignedconstunsigned& num2){    if00)        return (num1 > num2) ? num1 : num2;    return Gcd(num2, num1%num2);}//上面针对函数的传入参数进行了优化。有传值调用改为了const 引用。避免了参数的拷贝,但是这个算法并不是万能的,因为在循环的算法中我们是不允许修改const属性的值。所以我们使用右值引用写一下。
2.0
//In the code below I have modified the return type and the parameter type passed in. Changed to an rvalue reference type. The //so-called rvalue reference is a reference to the right value. All we've learned before are lvalue references. The //left-to-right value distinction can be judged by the address. There is a move semantics associated with the rvalue, which enables the transfer of resources rather than copying them. //See an example of an image: a refrigerator with an elephant in it and now wants to get the elephant in another refrigerator. It was previously done by copying an elephant and then putting the cloned elephant in the freezer and destroying the original elephant. But with the rvalue reference, it's our practice to throw the elephant out of the first refrigerator and put it in the second refrigerator. At least one less copy and destroy process. As far as details are not discussed, after all, the focus of writing is the algorithm. unsigned&& GCD (unsigned&AMP;&AMP;NUM1,unsigned&AMP;&AMP;NUM2) {if(NUM1 = =0|| Num2 = =0)return STD:: Move ((Num1 > Num2)? num1:num2); while(Num2 >0){intrem = num1%num2;        NUM1 = num2;    num2 = REM; }STD::cout<<"Call the second function"<<STD:: Endl;return STD:: Move (NUM1);}//About rvalue reference nagging last sentence. A universal reference type (Universial reference) exists only if there is a type inference.
The final optimization:
value){    if00){        value = (num1 > num2) ? num1 : num2;        returnvalue;    }    while0){        int rem = num1%num2;        num1 = num2;        num2 = rem;    }    value=num1;    returnvalue;}//上面我传入了一个value参数,用于实现左值引用。

Code:

#include <iostream>intGCD (const unsigned& NUM1, const unsigned& num2) {if(NUM1 = =0|| Num2 = =0)return(Num1 > Num2)? num1:num2;returnGCD (num2, NUM1%num2);} /*intGCD (unsigned &num1, unsigned &num2) {if(NUM1 = =0|| Num2 = =0)return(Num1 > Num2)? num1:num2; while(Num2 >0){intREM = NUM1%num2;        NUM1 = num2;    num2 = REM; } std::cout <<"Call the first function"<< Std::endl;returnNUM1;}*/unsigned& Gcd (unsigned &&num1, unsigned &&num2,unsigned & value) {if(NUM1 = =0|| Num2 = =0) {value = (Num1 > num2)? num1:num2;returnValue } while(Num2 >0){intREM = NUM1%num2;        NUM1 = num2;    num2 = REM; } value=num1;returnValue;} /*int&& GCD (const unsigned &num1,const unsigned &num2) {if(NUM1 = =0|| Num2 = =0)returnStd::move ((Num1 > Num2) num1:num2);intGCD =0; for(inti =1; I <int(NUM1); ++i) {if((NUM1%i==0) && (num2%i==0)) GCD = i;Else            Continue; }returnStd::move (GCD);}*/intMain () {//STD:: cout << Test_func ( -, -);//Std::cout << GCD ( +, -);intNUM1 = +, num2 = -; unsigned gcd =0; GCD ( +, A, gcd) =5;//STD:: cout << GCD ( +, A, GCD);system("Pause");return 0;}

The greatest common divisor of algorithm hand-over operation

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.