欧几里得算法和扩展的欧几里得算法C++递归实现

19 篇文章 21 订阅
12 篇文章 1 订阅

欧几里得算法和扩展的欧几里得算法C++递归实现

关于欧几里得算法的流程不再赘述,不清楚的可以搜得到。本篇主要通过C++代码利用递归的思想实现,参考书籍是《密码编码与信息安全:C++实践》。

1、欧几里得算法实现

欧几里得算法比较简单,主要用于求两个数(多项式)的最大公因数(式),直接上代码。

#include <iostream>
using namespace std;
int Euclidean(int a, int b){
	if(b==0){
		return a;
	}else{
		return Euclidean(b, a%b);
	}
}

2、扩展的欧几里得算法实现

扩展的欧几里得算法主要用于求模逆运算。我第一次实现扩展的欧几里得算法是通过辗转相除,然后再回溯求出了 a a a b b b的系数。感觉可以像普通欧几里得算法一样可以递归编程,但是总是没想出来。后来借助参考书实现了。主要思想是要写出此时的递推关系式。

2.1递推关系式的说明

扩展欧几里得算法本质上是要求得
a × s + b × t = g c d a\times s+b\times t=gcd a×s+b×t=gcd
这个式子。按照递归的思想,我们应该这么考虑:要利用递归得到 a a a b b b的式子,结合辗转相除的原理,我们肯定是先得到 b b b a ( m o d b ) a\pmod b a(modb)的关系式。假设已经有了
s ′ × b + t ′ × ( a ( m o d b ) ) = g c d s'\times b+t'\times (a\pmod b)=gcd s×b+t×(a(modb))=gcd

如何得到我们需要的式子?

做一个简单的变形就出来了:我们知道 a ( m o d b ) = a − ⌊ a / b ⌋ ∗ b a\pmod b=a-\lfloor a/b\rfloor *b a(modb)=aa/bb,代进上式,有
s ′ × b + t ′ × ( a ( m o d b ) ) = s ′ × b + t ′ × ( a − ⌊ a / b ⌋ ∗ b ) = t ′ × a + ( s ′ − t ′ × ⌊ a / b ⌋ ) × b = g c d s'\times b+t'\times (a\pmod b)=s'\times b+t'\times (a-\lfloor a/b\rfloor *b)\\ =t'\times a+(s'-t'\times \lfloor a/b\rfloor)\times b =gcd s×b+t×(a(modb))=s×b+t×(aa/bb)=t×a+(st×a/b)×b=gcd
所以递归就是根据这个式子编出的。具体代码如下:

#include <iostream>
using namespace std;
struct ExtEuc
{
	int gcd;
	int s;
	int t;
};
//the most important is the "recursion formula",especially in Extends_Eclidean
ExtEuc Extends_Eclidean(int a, int b){
	ExtEuc obj, obj1;
	if(b == 0){
		obj1.gcd = a;
		obj1.s = 1;
		obj1.t = 0;
		return obj1;
	}
	obj1 = Extends_Eclidean(b, a%b);
	//attention!!
	obj.gcd = obj1.gcd;
	obj.s = obj1.t;
	obj.t = obj1.s - (a/b)*obj1.t;
	return obj;
}

3、整体代码如下

#include <iostream>
using namespace std;
struct ExtEuc
{
	int gcd;
	int s;
	int t;
};

//the most important is the "recursion formula",espeacially in Extends_Eclidean
int Euclidean(int a, int b){
	if(b==0){
		return a;
	}else{
		return Euclidean(b, a-(a/b)*b);
	}
}

ExtEuc Extends_Eclidean(int a, int b){
	ExtEuc obj, obj1;
	if(b == 0){
		obj1.gcd = a;
		obj1.s = 1;
		obj1.t = 0;
		return obj1;
	}

	obj1 = Extends_Eclidean(b, a%b);
	//attention!!
	obj.gcd = obj1.gcd;
	obj.s = obj1.t;
	obj.t = obj1.s - (a/b)*obj1.t;
	return obj;
	
}

int main(int argc, char const *argv[])
{
	/* code */
	ExtEuc f = Extends_Eclidean(2,3);
	cout<<f.s<<"*2+"<<f.t<<"*3="<<f.gcd<<"  "<<endl;
	return 0;
}

[1] 王静文, 吴晓艺. 密码编码与信息安全:C++实践[M]. 清华大学出版社, 2015.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值