求最大公约数(欧几里得算法)
1.辗转相除法之所以有效是因为其基于一个核心原理,即:
两个数的最大公约数等于其中较小的数字和二者之间余数的最大公约数
https://zhuanlan.zhihu.com/p/324578532?ivk_sa=1024320u9o
2.伪代码
set num1 to O;
set num2 to O;
write two numbers:
a = max{num2,num1}
b = min{num2,num1}
c = a%b
d = a-b*c
if(c !=0)
num1 =b
num2 =d
write"b is the greatest common divisor"
3.走查
a = 72
b = 40
c = 1
d = 32
a = 40
b = 32
c = 1
d = 8
a = 32
b = 8
c = 0
最大公约数为8;
a = 75
b = 40
c = 1
d = 35
a = 40
b = 35
c = 1
d = 5
a = 35
b = 5
c = 0
最大公约数为5。