当前位置:网站首页>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;
}
边栏推荐
- Use of collection framework
- 2022年郑州轻工业新生赛题目-打死我也不说
- B1022 D进制的A+B
- JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter
- DBUtils
- 树莓派rtmp推流本地摄像头图像
- Raspberry pie RTMP streaming local camera image
- Basic operation of vim
- Demo of throttling function -- regular expression matching
- Acticiti中startProcessInstanceByKey方法在variable表中的如何存储
猜你喜欢

34. 分析flexible.js

Detailed description of polymorphism

JVM上篇:内存与垃圾回收篇五--运行时数据区-虚拟机栈

JVM part I: memory and garbage collection part II -- class loading subsystem

JDBC API 详解
![[Niuke discussion area] Chapter 7: building safe and efficient enterprise services](/img/62/2b170e8dd5034708aebc6df0bc2b06.png)
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services

Explore the mysteries of the security, intelligence and performance of the universal altek platform!

What should test / development programmers over 35 do? Many objective factors

JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈

Translation of robot and precise vehicle localization based on multi sensor fusion in diverse city scenes
随机推荐
简化JDBC的MyBits框架
JVM Part 1: memory and garbage collection part 14 -- garbage collector
素数筛选(埃氏筛法,区间筛法,欧拉筛法)
来自“飞人”乔丹的启示!奥尼尔开启的另一个“赛场”
ssm框架整合
SSM framework integration
Idea remote debugging
数据库连接池&&Druid使用
枚举类实现单例模式
35. Scroll
《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译
Use ngrok for intranet penetration
The interface can automatically generate E and other asynchronous access or restart,
Install pyGame
JVM Part 1: memory and garbage collection part 8 - runtime data area - Method area
秒杀系统设计
B1027 print hourglass
SQL数据库→约束→设计→多表查询→事务
B1025 reverse linked list*******
Scientific Computing Library - numpy