C language--seeking greatest common divisor and least common multiple

Source: Internet
Author: User
Tags greatest common divisor

Basic concepts
    1. Least common multiple: multiples of two or more integers that are public are called their common multiple. The least common multiple of the integer A, a, and the same as [A, b], the a,b,c least common multiple is recorded as [A,b,c], and the least common multiple of multiple integers have the same notation.
    2. Greatest common divisor: Also known as greatest common divisor, the largest common factor, refers to two or more integers total of the largest one. The greatest common divisor of A, B is recorded as (A, b), the same as the greatest common divisor of a,b,c (A,B,C), and the greatest common divisor of multiple integers have the same notation.
    3. With regard to least common multiple and greatest common divisor, we have such a theorem: (A, b) [A,b]=ab (A, b are integers).
Method Analysis Greatest Common divisor
    1. Euclidean method:
      Set two to A, B (a≥b), and the steps for A and B greatest common divisor are as follows:
      ①a%b remainder C
      ② if c=0, then B is a two-digit greatest common divisor
      ③ if c≠0, then a=b,b=c, then go back to execute ①
    2. More subtractive methods:
      The steps for A and B greatest common divisor are set at two for A and B, as follows:
      ① if a>b, then A=a-b
      ② If a
Least common multiple
    1. (A, B) [A,b]=ab
      The first calculation of AB and [A, b] Division is least common multiple.
    2. Poor lifting method:
      Set two numbers to A, B (a≥b), t=a,i=1 the steps for a and b least common multiple are as follows:
      ①a%b remainder C
      ② if c=0, then A is a two-digit least common multiple
      ③ if c≠0, then i=i+1,a=t*i, then go back to execute ①
Code implementation Greatest common divisor
    1. Euclidean method:
int HCD(int x, int y){int temp;if (x < y)  //如果x<y交换x,y{temp = x;x = y;y = temp;}while (x%y) //x%y!=0时 {temp = x;    x = y;  //将y赋给xy = temp % y;   //余数赋给y}//直到x%y == 0时y为最大公约数return y;}
    1. More subtractive methods:
int HCD(int x, int y){    int MAX = x > y ? x : y;      int MIN = x < y ? x : y;    int TEMP = MAX - MIN;    if (TEMP == 0)        return MAX;   //递归终止    else        HCD(TEMP, MIN); //递归}
Least common multiple
    1. (A, B) [A,b]=ab
//求最大公约数 辗转相除法int HCD(int x, int y){int temp;if (x < y)  //如果x<y交换x,y{temp = x;x = y;y = temp;}while (x%y) //x%y!=0时 {temp = x;x = y;  //将y赋给xy = temp % y;   //余数赋给y}//直到x%y == 0时y为最大公约数return y;}//求最小公倍数(a,b)[a,b]=abint ICM2(int x, int y){return x*y / HCD2(x, y);}
    1. Poor lifting method:
//求最小公倍数int ICM(int x, int y){int temp;int i = 1;if (x < y)  //如果x<y交换x,y{temp = x;x = y;y = temp;}temp = x;while (x%y) //x%y!=0时{i++;x = temp * i;//将x*1、x*2...赋给x}//直到x%y == 0时x为最小公倍数return x;}
Reference article:
    1. Common algorithm: C language to seek least common multiple and greatest common divisor three kinds of algorithms--csdn
    2. C Language Computing 2 numbers of least common multiple--blog Park
    3. Baidu Encyclopedia

C language--seeking greatest common divisor and least common multiple

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.