当前位置:网站首页>Rolling Division
Rolling Division
2022-07-27 05:21:00 【Ye Chen】
division
Don't talk much , The strongest code first :
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a % b);
}
The return value of this function is a and b Maximum common divisor of
Introduction of rolling division
division algorithm You can find the greatest common divisor , seeing the name of a thing one thinks of its function , Divide repeatedly , Finally, we get the greatest common divisor of the two numbers .
First, let's analyze the theorem :
Theorem : The greatest common divisor of two integers is equal to the lesser one and the greatest common divisor of the remainder . greatest common divisor (Greatest Common Divisor) Abbreviation for GCD.
gcd(a,b) = gcd(b,a mod b) ( Might as well set a>b And r=a mod b ,r Not for 0)
Words are hard to understand , For example :
134 / 18 = 7 … 8
18 / 8 = 2 … 2
8 / 2 = 4 … 0
See , Is it exactly the same as the theorem , So we need to find the greatest common divisor , and 134 /18 And 8 / 2 The greatest common divisor of is equal to , So we just need to find out 8 / 2 Maximum common divisor of , Is that what I said at the beginning? Change two numbers and then find , And we need to know , Because two numbers are divided , Remainder is 0, Its divisor must be the greatest common divisor , So here 2 That's what we're looking for 138 / 18 Maximum common divisor of . As for the proof , Baidu search , There are . The main proof idea is , Yes a/b more than r In the form of , To assume that y by a,b The common factor of , Prove again b,r The common divisor of is also y.
The last flow chart :
Code implementation :
int gcd(int a,int b){
int r=1;// remainder , The initial value of the assignment is 1
while(r!=0){
// If a<b, There is no need to reverse ab, Quotient in calculation 0 Codivisor itself , It can be reversed in the next operation
r = a % b;
a = b;
b = r;
}
return a;
}
边栏推荐
- 求组合数(最强优化)
- Another skill is to earn 30000 yuan a month+
- B1031 check ID card
- Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
- mq设置过期时间、优先级、死信队列、延迟队列
- Basic operation of vim
- SSM framework integration
- Use ngrok for intranet penetration
- 学生管理系统
- JVM Part 1: memory and garbage collection part 10 - runtime data area - direct memory
猜你喜欢

SQL数据库→约束→设计→多表查询→事务

Derivation and explanation of PBR physical illumination calculation formula

Database design - relational data theory (ultra detailed)

JVM Part 1: memory and garbage collection part 12 -- stringtable

34. Analyze flexible.js

Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?

JVM上篇:内存与垃圾回收篇八--运行时数据区-方法区

JVM Part 1: memory and garbage collection part 8 - runtime data area - Method area

JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter

How to sinicize the JMeter interface?
随机推荐
JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter
探寻通用奥特能平台安全、智能、性能的奥秘!
JVM Part 1: memory and garbage collection part 7 -- runtime data area heap
The difference between strlen and sizeof
JVM上篇:内存与垃圾回收篇十二--StringTable
Bean的生命周期&&依赖注入*依赖自动装配
Card drawing program simulation
Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
如何快速有效解决数据库连接失败问题
笔记系列之docker安装Postgresql 14
JVM Part 1: memory and garbage collection part 14 -- garbage collector
B1030 完美数列
SSM framework integration
redis发布订阅模式
Find the number of combinations (the strongest optimization)
辗转相除法
How idea creates a groovy project (explain in detail with pictures and texts)
The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay
B1023 组个最小数
秒杀系统设计