歐幾裡得算法 解決的問題是:尋找兩個給定的正整數m和n的最大公約數
下面是C#代碼的 歐幾裡得算法
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
public int MaxDivisor(int a, int b)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170795.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170884.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int max=a>=b?a:b;//得到最大數
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int min=a<b?a:b;//得到最小數
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int r=1; //余數
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
while (r> 0)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170826.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
r = max % min; //求模
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
max = min;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
min = r;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170804.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
return max;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170985.gif)
}
歐幾裡得算法的擴展,問題描述如下:
尋找兩個給定的正整數m和n的最大公約數d 和兩個整數,使得a*m+b*n=d
下面是C#的 歐幾裡得算法擴展的代碼
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
public int MaxDivisorex(int sum1, int sum2, out int a, out int b)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170795.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170884.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int tmp_b=a = 0;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int tmp_a=b = 1; //tmp_b tmp_a 是 輔助變量
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int max = sum1 >= sum2 ? sum1 : sum2;//得到最大數
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int min = sum1 < sum2 ? sum1 : sum2;//得到最小數
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int q = 0; //商
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int r = 1; //余數
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
q = max / min;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
r = max % min;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
max = min;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
min = r;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
while (r > 0)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170826.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
int tmp = tmp_a;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
tmp_a = a;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
a = tmp - q * a;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
tmp = tmp_b;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
tmp_b = b;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
b = tmp - q * b;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
q = max / min;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
r = max % min;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
max = min;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
min = r;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170804.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170849.gif)
return max;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170985.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
結果驗證
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
int a, b;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
int r = MaxDivisorex(1769, 551, out a, out b);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311170733.gif)
MessageBox .Show(string.Format ("{0} * 1769 + {1} *551 = {2}",a,b,(a*1769+b*551)));這個算法不是很復雜,不過要清楚還真是費事,等我可以解釋得很簡單的時候我再解釋好了,現在相當於我自己的讀書筆記。
如果有人能簡單的解釋出來的話,也麻煩留下言。謝謝。。。