当前位置:网站首页>greatest common divisor
greatest common divisor
2022-07-24 04:37:00 【Hi hi】
List of articles
【C Language – The road of big men 】
List of articles
1. What is a common divisor ?
Common divisor , Also known as “ The common factor ”. It is a number that can divide several integers at the same time . If an integer is several integers at the same time Divisor , Call this integer their “ Common divisor ”.
2. greatest common divisor
The largest of the common divisors is called the greatest common divisor . For any number of positive integers ,1 Their common factor is always . The common divisor is opposite to the common multiple , Is both A The divisor of is also B The divisor of ,12 and 15 The common divisor of is 1,3, The greatest common divisor is 3. Another example ,30 and 40, Their common divisors are 1,2,5,10, The greatest common divisor is 10
3. The relationship between the greatest common divisor and the least common multiple :
The product of two numbers / greatest common divisor = Minimum common multiple
4. Problem solving guidance
Such as 18 and 6, We can know that the greatest common divisor of two numbers must be less than or equal to the smallest one , Then to achieve the greatest common divisor , You must first find the smallest of the two numbers
And then from 6 Or ratio 6 Find the least common divisor among small numbers

5. Code display :
The code is as follows ( Example ):
#include<stdio.h>
int main() {
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int min = (m < n ? m : n);
while (1)
{
if (m % min == 0 && n % min == 0)
{
printf("%d", min);
break;
}
min--;
}
return 0;
}
Of course, there is more than one method , This method is inefficient
6. division
The introduction is shown in the figure :
Pictured , use 24 except 18 Take the remainder 6
use 18 except 6 Take remainder as 0
6 Is the greatest common divisor of these two numbers
As shown in the figure above, if we put 24 regard as m, hold 18 regard as n, If the remainder is not 0, will n The value is assigned to m, The value of the remainder is assigned to n
Remainder if yes 0,n Is the greatest common divisor
7. Code demonstration :
#include<stdio.h>// greatest common divisor
int main() {
int m = 0;
int n = 0;
int r = 0;
scanf("%d %d", &m, &n);
while (r = m % n)
{
m = n;
n = r;
}
printf("%d", n);
return 0;
}
summary
The contents involved here are quite basic , If you don't know some syntax in the above code , It shows that the foundation is too weak
You can read my series of articles 【C】 C Introduction to language
Of course, as long as you have a clear idea , This kind of code is still very easy to write .
边栏推荐
- What is the real HTAP? (2) Challenge article
- Godson leader spits bitterness: we have the world's first performance CPU, but unfortunately no one uses it!
- 你有多久没有换新手机了?
- Ambire gas tank launches exclusive NFT launch
- Energy principle and variational method note 11: shape function (a dimension reduction idea)
- 致-.-- -..- -
- 归并排序(Merge sort)
- The judges of C language classic exercises score the highest and lowest to get an average score
- Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind
- C language: selective sorting method
猜你喜欢

64 attention mechanism 10 chapters

Chery arizer 8 products are powerful, and "all excellent" is not for nothing

Logback log framework technology in project development

Forward proxy, reverse proxy and XFF

Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base

-Bash: wget: command not found

The second anniversary of opengauss' open source, cracking the pain point of database ecology

Array force buckle (continuously updated)

C language classic exercises to write a program to find all the perfects within 1000.

Merge sort
随机推荐
Unable to delete the file prompt the solution that the file cannot be deleted because the specified file cannot be found
Share a login interface template of QT implementation
IP second experiment mGRE OSPF
Solution to the prompt of online account opening and transfer in stock speculation that the depository and transfer services are not activated (China Merchants Bank)
激活函数和最常用的10个激活函数
C language: generation of random numbers
Game improvement of smart people: Chapter 3 Lesson 3: find game
C语言:冒泡排序法
Will your NFT disappear? Dfinity provides the best solution for NFT storage
Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc
The C host is always set separately for IIC. If enough, the next few bits can be set
Nautilus 3.19.2 adds momentum to Gnome
基于GL Pipeline与光线追踪技术的融合实现的台球模拟器
最大公约数
Why can't I hide folders? Solutions to the hidden folders on the computer that can still be seen
Er system, in Lin reply bit, count, successfully open r com change
What is the general family of programmers working in first tier cities?
Billiard simulator based on the integration of GL pipeline and ray tracing technology
Alibaba Taobao Department interview question: how does redis realize inventory deduction and prevent oversold?
-Bash: wget: command not found