Seeking greatest common divisor by the method of dividing

Source: Internet
Author: User
Tags greatest common divisor

The Euclidean division of the greatest common divisor is one of the best algorithms for solving the two numbers of the two.

Algorithm principle: If the remainder of a divided by B is r, then there is (A, b) = (b, R) ((A, a) ((a) greatest common divisor of A and B)

Example: The greatest common divisor solution process for 169 and 48

169 = 3 + --(169, 48) = (48, 25)

* 1 + --(48, 25) = (25, 13)

25 = 13 * 1 + 12

13 = 12 * 1 + 1

1 * + 0 --(12, 1) = 1

So greatest common divisor is 1


The following is a C + + implementation of the algorithm, using the recursive algorithm

#include <iostream> #include <conio.h>using namespace std;int gcd (int a, int b) {return b = = 0? a:gcd (b, a% b);} int main () {cout << gcd (169) << Endl;_getch (); return 0;}


Seeking greatest common divisor by the method of dividing

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.