当前位置:网站首页>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 !
边栏推荐
- What is the combined effect of compose and recyclerview?
- -bash: wget: 未找到命令
- Pytorch simple sample summary
- 【管理篇 / 升级】* 02. 查看升级路径 * FortiGate 防火墙
- 推荐系统专题 | 推荐系统架构与单域跨域召回模型
- SQL用户表的通用设计
- Airserver third party projection software v7.3.0 Chinese Version (airplay terminal utility)
- LiveQing直播RTMP点播视频流媒体平台如何携带登录接口返回的sid和token接口调用鉴权streamToken视频流鉴权
- BGP機房的優點
- Regular expression conversion to corresponding text gadget
猜你喜欢

IDM downloader free high-quality win download tool without restrictions

Summary of some open source libraries that drive MCU hardware debugger (including stlink debugger)

Software testing interview ideas, skills and methods to share, learn is to earn

-bash: wget: 未找到命令

Const char* in vs2022 cannot assign char*

Talk about HART Protocol

涨薪神器

PMP备考心得 | 好的习惯、好的过程、好的结果

LiveQing直播点播流媒体OBS推流直播如何获得接口校验token视频校验streamToken及配置token有效期
银联最新测试工程师笔试题目,你能得多少分?
随机推荐
openresty lua-resty-balancer动态负载均衡
Avantages de la salle des machines bgp
Implementation of OA office system based on JSP
LiveQing直播RTMP点播视频流媒体平台如何携带登录接口返回的sid和token接口调用鉴权streamToken视频流鉴权
JMeter --- JMeter installation tutorial
FasterRCNN示例代码测试1:令anchor_generator = None
2302. Count the number of subarrays with a score less than k - sliding array - double hundred code
[cloud native] in the era of storm and cloud, the sharp edge of DBAs is out of the sheath
提升从改变开始...
基于时频图的音频处理-matlab
在通达信开户安全不
正则表达式转换为相应的文字小工具
Regular expression conversion to corresponding text gadget
DOM series prohibit selected text and prohibit right-click menu
Internet download manager is simply a killer of downloaders
【并发编程】第二章:从核心源码深入ReentrantLock锁
Is it safe to open an account online? How about Galaxy Securities
php获取证书编号没有serialNumberHex只有serialNumber处理方法
C语言经典练习题(1)——“水仙花数“
股票开户网上开户安全吗,银河证券怎么样