-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuclidAlgorithm.cpp
More file actions
56 lines (52 loc) · 1.18 KB
/
Copy pathEuclidAlgorithm.cpp
File metadata and controls
56 lines (52 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Extension of Euclid algorithm__black books.
*/
#include <iostream>
using namespace std;
typedef unsigned int uInt;
// 递归欧几里德算法
uInt gcd1(uInt x, uInt y) {
if (x < y) return gcd1(y, x);
if (0 == y) return x;
return gcd1(y, x % y);
}
// 迭代欧几里德算法
uInt gcd2(uInt x, uInt y) {
while (y) {
uInt tmp = y;
y = x % y;
x = tmp;
}
return x;
}
// 减法求最大公约数
uInt gcdSub(uInt x, uInt y) {
if (x < y) return gcdSub(y, x);
if (y == 0) return x;
else return gcdSub(y, x - y);
}
// Stein算法求最大公约数
uInt gcdStein(uInt x, uInt y) {
if (x < y) return gcdStein(y, x);
if (y == 0) return x;
else {
if (x % 2 == 0) {
if (y % 2 == 0)
return gcdStein(x >> 1, y >> 1) << 1;
else return gcdStein(x >> 1, y) ;
}
else {
if (y % 2 == 0)
return gcdStein(x, y >> 1);
return gcdStein(y, x - y);
}
}
}
int main(void)
{
int n, m;
cin >> n >> m;
cout << gcd1(n, m) << endl << gcd2(n, m) << endl;
cout << gcdSub(n, m) << endl << gcdStein(n, m) << endl;
return 0;
}