当前位置:网站首页>Solve the greatest common divisor and the least common multiple
Solve the greatest common divisor and the least common multiple
2022-07-23 09:18:00 【A Liang joy】
Catalog
Find the greatest common divisor
Find the least common multiple
2. The greatest common divisor method
Find the greatest common divisor
Definition of the greatest common divisor : If there is a natural number a Can be Natural number b to be divisible by , said a by b Multiple ,b by a The divisor of . The common divisor of several natural numbers , It's called the common divisor of these natural numbers . The greatest common divisor of the common divisor , It is called the greatest common divisor of these natural numbers . example : stay 2、4、6 in ,2 Namely 2,4,6 Maximum common divisor of .
Knowing the definition of the greatest common divisor , Let's learn how to solve the greatest common divisor .
1. Violent solution
Ideas : Suppose there are two numbers m and n, First find the smaller of the two numbers min, And then use it while Loop to find out m and n Maximum common divisor of .while The cycle body of the cycle is if m and n to be divisible by min Zero at the same time , So the min Namely m and n Maximum common divisor of ; If this condition is not met min Self subtraction one .
Code implementation :
#include <stdio.h>
int main()
{
int m, n;
printf(" Please enter two integers :>");
scanf("%d %d", &m, &n);
int min = (m < n ? m : n);
while (1)
{
if ((m % min == 0) && (n % min == 0))
{
break;
}
min--;
}
printf("%d and %d The greatest common divisor of %d\n", m, n, min);
return 0;
}Output results :

2. division
division , also called Euclidean algorithm (Euclidean algorithm), Is to ask for two Positive integer Algorithm of the greatest common divisor of . It is the oldest algorithm known , It dates back to... BC 300 Years ago . Its concrete method is : Divide the larger number by the smaller number , And then the remainder that appears ( The first remainder ) Remove divisor , And then the remainder that appears ( Second remainder ) Remove the first remainder , So again and again , Until the end, the remainder is 0 until . If you want to find the greatest common divisor of two numbers , So the final divisor is the greatest common divisor of these two numbers .
Code implementation :
#include <stdio.h>
int main()
{
int m, n;
printf(" Please enter two integers :>");
scanf("%d %d", &m, &n);
int m1 = m;
int n1 = n;// preservation m and n The data of
int r = 0;
while (r = m % n)
{
m = n;
n = r;
}
printf("%d and %d The greatest common divisor of %d\n", m1, n1, n);
return 0;
}
Output results :

Find the least common multiple
Definition of least common multiple : If a number is both a again b Multiple , Then let's call this number a and b The common multiple of , If this number is in a b Is the smallest of all common multiples of , Then this number is the least common multiple . for example ,10 and 20 The minimum common multiple of is 20.
1. Violent solution
Suppose there are two numbers m and n, First find the maximum of the two numbers max, And then use it while Loop to find out m and n The least common multiple of .while The cycle body of the cycle is if max to be divisible by m and n Zero at the same time , So the max Namely m and n The least common multiple of ; If this condition is not met max Self plus one .
Code implementation :
#include <stdio.h>
int main()
{
int m, n;
printf(" Please enter two numbers :>");
scanf("%d %d", &m, &n);
int max = (m > n ? m : n);
while (1)
{
if ((max % m == 0) && (max % n == 0))
{
break;
}
}
printf("%d and %d The minimum common multiple of is %d\n", m, n, max);
return 0;
}Output results :

2. The greatest common divisor method
Suppose there are two numbers m and n, And m and n The greatest common divisor of is x、 Minimum common multiple y, Then there is a relationship between the least common multiple and the greatest common divisor :m * n = x * y. For example, this relation , We can implement our code .
Code implementation :
#include <stdio.h>
int main()
{
int m, n;
printf(" Please enter two numbers :>");
scanf("%d %d", &m, &n);
int m1 = m;
int n1 = n;
int r = 0;
while (r = m % n)
{
m = n;
n = r;
}
printf("%d and %d The minimum common multiple of is %d\n", m1, n1, m1 * n1 / n);
}Output results :

This blog mainly introduces two common methods of the greatest common divisor and least common multiple . If you are still interested in other methods , You can search for , I won't repeat it here . Last , If you think you've got something , You can point a favor to support !
边栏推荐
- 提升从改变开始...
- 服务器托管、服务器租用、云主机各自的优势
- Stream操作之 先分组再取最大值
- How many points can you get on the latest UnionPay written test for test engineers?
- 认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法
- How many of the 50 classic computer network interview questions can you answer? (III)
- . net to develop cloud native applications, you only need to add oil to yourself
- 1646. Recursive method of getting the maximum value in the generated array
- Go-Excelize API源码阅读(五)—— Close()、NewSheet()
- How many of the 50 classic computer network interview questions can you answer? (top)
猜你喜欢
随机推荐
在通达信开户安全不
[cann training camp] learning notes - Comparison between diffusion and Gan, dalle2 and Party
1646. 获取生成数组中的最大值递归法
Learn the distributed architecture notes sorted out by Alibaba in one month
求解最大公约数和最小公倍数
Huawei applications have called the checkappupdate interface. Why is there no prompt for version update in the application
Online matting and background changing and erasing tools
解析创客教育活动所需的空间实践场
Template school jumpserver security operation and maintenance audit screen
transformer汇总
openresty lua-resty-balancer动态负载均衡
涨薪神器
服务器托管、服务器租用、云主机各自的优势
BGP機房的優點
关系表达式 大于> 小于< 全等=== Nan isNan() 逻辑运算符 双感叹号!! && || % ++ -- 短路计算 赋值表达式 快捷运算符 顺序 闰年
It is not safe to open an account in tongdaxin
十. 实战——云服务器
JMeter --- JMeter installation tutorial
Implementation of OA office system based on JSP
C语言经典练习题(1)——“水仙花数“









