# Common algorithms: C language for least common multiple and greatest common divisor three kinds of algorithms

Source: Internet
Author: User

Least common multiple: a concept in number theory, where a multiple of two integers becomes their common multiple, and one of the smallest common multiple is their least common multiple, in the same way that the smallest positive integers in multiple public multiples of several integers are called their least common multiple, Wikipedia: Define Click to open link

least common multiple algorithm :

least common multiple = product of two integers ÷ Greatest Common Divisor

greatest common Divisor algorithm :

(1) Euclidean method

There are two integers a and the b :

① a%b to the remainder C

② if c=0 , you b that is, the greatest common divisor of two numbers.

③ If c≠0 , you a=b , B=c , and then go back to running ①

For example, "27 and 15

27÷15 Yu A 15÷12 Yu 3 12÷3 Yu 0 therefore, 3 that is the greatest common divisor

`#include <stdio.h>void main ()/*/greatest common divisor =/  {    int m, n, a, B, T, C;   printf ("Input, Integer numbers:\n");   scanf ("%d%d", &a, &b);   M=a;   n=b;   while (b!=0)/  * Remainder is not 0, continue to divide until the remainder is 0 */    {c=a%b; a=b;  B=c;}   printf ("The largest common divisor:%d\n", a);   printf ("The least common multiple:%d\n", m*n/a);}`

⑵ Phase Subtraction

There are two integers a and B:

① if a>b, then A=a-b

② if a<b, then b=b-a

③ if a=b, a (or b) is a two-digit greatest common divisor

④ if a≠b, then go back to run ①

For example, the greatest common divisor process for 27 and 15 is:

27-15=12 (15>12) 15-12=3 (12>3)

12-3=9 (9>3) 9-3=6 (6>3)

6-3=3 (3==3)

Therefore, 3 is the greatest common divisor

`#include <stdio.h>void main ()/  * Subtract greatest common divisor */{     int m, n, a, b, C;   printf ("Input, Integer numbers:\n");   scanf ("%d,%d", &a, &b); m=a; n=b;      /* A, B is unequal, and the large number decreases until it is equal. *    /while (a!=b)          if (a>b)  a=a-b;      else  b=b-a;   printf ("The largest common divisor:%d\n", a);   printf ("The least common multiple:%d\n", m*n/a);}`

⑶ Poor Lifting method

There are two integers a and B:

①i=1

② If a, B can be divisible by I at the same time, then t=i

③i++

④ If I <= a (or b), then go back to run ②

⑤ If i > A (or b), then T is greatest common divisor, ending

Improved:

①i= A (or b)

② If a, B can be divisible by I at the same time, then I is greatest common divisor,

End

③i--, go back and run ②.

There are two integers a and B:

①i=1

② If a, B can be divisible by I at the same time, then t=i

③i++

④ If I <= a (or b), then go back to run ②

⑤ If i > A (or b), then T is greatest common divisor, ending

Improved:

①i= A (or b)

② If a, B can be divisible by I at the same time, then I is greatest common divisor,

End

③i--, go back and run ②.

`#include <stdio.h>void main ()/  * Exhaustive method for greatest common divisor */{     int  m, n, a, B, I, t;   printf ("Input, Integer numbers:\n");   scanf ("%d,%d", &a, &b); m=a;  n=b;    for (I=1; i<= A; i++)         if (a%i = = 0 && b%i ==0)    t=i;   printf ("The largest common divisor:%d\n", t);   printf ("The least common multiple:%d\n", m*n/t);}  /* Improved for   (t= A; t>0; t--)           if (a%t = = 0 && b%t ==0) break    ; */`

`The method of least common multiple for     (i= A;; i++)         if (i% A = = 0 && i% b ==0) break     ;     printf ("The least common multiple:%d\n", i)//multiple numbers of greatest common divisor and least common multiple for     (i= A; i>0; i--)         if (a%i==0&&b%i==0 &&c%i==0) break     ;     printf ("The largest common divisor:%d\n", i);     for (i= A;; i++)         if (i%a==0&&i%b==0&&i% c==0) break    ;     printf ("The least common multiple:%d\n", i)`

Common algorithms: C language for least common multiple and greatest common divisor three kinds of algorithms

Related Keywords:
Related Article

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.