C language division: Calculate the minimum common divisor of two numbers, and divide the common divisor.

Source: Internet
Author: User

C language division: Calculate the minimum common divisor of two numbers, and divide the common divisor.

The maximum division of the moving phase is used to calculate the maximum common divisor of two numbers.

(A, B) is used to represent the maximum common divisor of a and B.
Theorem: We know that a, B, and c are positive integers. If a is divided by B, c (a, B) = (B, c ). (Please refer to other materials for the certification process)

For example, calculate the maximum common divisor of 15750 and 27216.
Solution:
Listen 27216 = 15750X1 + 11466 listen (15750,27216) = (15750,11466)
Listen 15750 = 11466 × 1 + 4284 listen (15750,11466) = (11466,4284)
Listen 11466 = 4284 × 2 + 2898 then (11466,4284) = (4284,2898)
Listen 4284 = 2898 × 1 + 1386 then (4284,2898) = (2898,1386)
Listen 2898 = 1386 × 2 + 126 then (2898,1386) = (1386,126)
Listen 1386 = 126 × 11 then (1386,126) = 126

So (15750,27216) = 126

The moving phase division method is suitable for finding the maximum common divisor of two relatively large numbers.

The Code is as follows:
# Include <stdio. h>
Int main ()
{
Int a, B, temp, x;
Scanf ("% d", & a, & B );
If (a> B)
{
Temp = B;
B =;
A = temp;
}

While (B %! = 0)
{
X = B %;
B =;
A = x;
}
Printf ("% d", );
}

Related Article

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.