C # seek GCD

Source: Internet
Author: User
Tags gcd

GCD a maximum integer that is divisible by two integers, for example, 24 and 152 GCD 3. Seeking GCD can be realized by remainder method. That is, the largest number in the two number is divided by the smallest number, and then the divisor is divided by the remainder, until the remainder is 0 o'clock, before the divisor is the GCD of two digits. The algorithm for calculating GCD is as follows:

public float Maxgongyueshu (int n1,int n2)
{
int temp = Math.max (n1, N2);
N2 = Math.min (n1, N2); the smallest of the two numbers in the//N2
N1 = Temp;//n1 in the largest of two digits
while (n2!=0)
{
N1 = n1 > n2? n1:n2;//make the number in N1 greater than the number in N2
int m=n1% N2;
N1 = n2;
N2 = m;
}
return N1;
}
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.