C language to find the greatest common divisor and the least common multiple

1. Tossing and dividing method : 

Also known as the Euclidean algorithm , it is the oldest known algorithm, dating back to 300 BC. 

Toss and turns: as the literal meaning is, it is to turn over and over again. Division is easy to understand, that is, to perform division.

The core of tossing and turning is to continuously divide two numbers. The principle is based on the fact that the greatest common divisor of two integers is equal to the greatest common divisor of the smaller number and the remainder of the division of the two numbers.

Suppose the two numbers are x, y.

shilling z = x % y ;

Then y is assigned to x i.e. x = y ;

Then assign z to y, that is, y = z;

Tossing and turning, the termination condition is: when y is equal to 0. 

code show as below:

[csharp]  view plain copy  
  1. #include<stdio.h>  
  2. intmain  ()  
  3. {  
  4.     int x, y, z, m, n;  
  5.     printf( "Please enter two numbers: " );  
  6.     scanf_s("%d%d", &x, &y);  
  7.     m = x, n = y;  
  8.     while (y != 0)  
  9.     {  
  10.         z = x%y;  
  11.         x = y;  
  12.         y = z;  
  13.     }  
  14.     printf( "The greatest common divisor is: %d\n" , x);  
  15.     printf( "The LCM is: %d\n" , m*n / x);  
  16.     system("pause");  
  17.     return 0;  
  18. }  


2. Tossing and subtracting :

That is , the Nicomanches method , which is characterized by a series of subtractions to obtain the greatest common divisor .

Subtraction is the continuous subtraction of two numbers.

Suppose the two numbers are x, y.

When x > y, let x = x - y;

Otherwise, let y = y - x;

After that, it has been reversed and subtracted until x = y, and it is terminated.

code show as below:

[csharp]  view plain copy  
  1. #include<stdio.h>  
  2. intmain  ()  
  3. {  
  4.     int x, y, m, n;  
  5.     printf( "Please enter two numbers: " );  
  6.     scanf_s("%d%d", &x, &y);  
  7.     m = x, n = y;  
  8.     while (x!=y)  
  9.     {  
  10.         if (x>y)  
  11.             x = x-y;  
  12.         else  
  13.             y = y-x;  
  14.     }  
  15.     printf("最大公约数是: %d\n", x);  
  16.     printf("最小公倍数是: %d\n", m*n / x);  
  17.     system("pause");  
  18.     return 0;  
  19. }  

3.穷举法:

穷举法的基本思想是根据题目的部分条件确定答案的大致范围,并在此范围内对所有可能的情况逐一验证,直到全部情况验证完毕。

穷举法又称枚举法,通过对数值范围内的所有数字进行检验,得出其结果。

代码如下:

[csharp]  view plain  copy
  1. #include<stdio.h>  
  2. int main()  
  3. {  
  4.     int x, y, i, m, n;  
  5.     printf("请输入两个数:");  
  6.     scanf_s("%d%d", &x, &y);  
  7.     m = x, n = y;  
  8.     for (i = 1; i <= x; i++)  
  9.     {  
  10.         if (x%i == 0 && y%i == 0)  
  11.     }  
  12.     for (i = x; i > 0; i--)  
  13.     {  
  14.         if (x%i == 0 && y%i == 0)  
  15.     }  
  16.     printf( "The greatest common divisor is: %d\n" , i);  
  17.     printf( "The LCM is: %d\n" , m*n / i);  
  18.     system("pause");  
  19.     return 0;  
  20. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324473915&siteId=291194637