Several algorithms of C language seeking GCD and LCM __ algorithm

Source: Internet
Author: User
Tags gcd integer numbers

Find LCM algorithm:

LCM = product of two integers gcd

Find GCD algorithm:

(1) Euclidean method

There are two integers a and B:

①a%b to the remainder C

② if c=0, then B is two number of GCD

③ if c≠0, then a=b,b=c, then go back to execute ①

For example, the GCD process for 27 and 15 is:

27÷15 Yu 1215÷12 more than 312÷3 0 so, 3 is GCD

[CPP] view plain copy #include <stdio.h> void main ()/* Euclidean algorithm gcd/{int m, n, a, B, T,      C      printf ("Input two integer numbers:\n");      scanf ("%d%d", &a, &b);   M=a;      N=b;  while (b!=0)/* The remainder is not 0, continue dividing 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, then a (or b) is a two-digit GCD

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

For example, the GCD 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 GCD

[CPP]  View Plain  copy #include <stdio.h>   void main  ( )   /*  Phase subtraction for GCD  */   {        int m, n, a, b,  c;       printf ("input two integer numbers:\n");       scanf  ("%d,%d",  &a, &b); m=a; n=b;         /* a, b are not equal, and large numbers are reduced until they are 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);  }&NBSP;&NBsp

⑶ method of poverty and lifting

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 execute ②

⑤ If i > A (or b), then T is GCD, end

Improved:

①i= A (or b)

② if a,b can be divisible by I at the same time, then I is GCD,

End

③i--, go back and execute ②.

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 execute ②

⑤ If i > A (or b), then T is GCD, end

Improved:

①i= A (or b)

② if a,b can be divisible by I at the same time, then I is GCD,

End

③i--, go back and execute ②.

[CPP]   View plain  copy #include <stdio.h>   void main  ()   /*  to seek GCD  */   {        int  m by exhaustive method, &NBSP;N,&NBSP;A,&NB

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.