-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd.cpp
138 lines (101 loc) · 2.8 KB
/
gcd.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <functional>
/*
C++ implementation of the Euclidian Algorithm, used
to find the greatest common divisor of two integers,
using a recursive solution.
The greatest common divisor, 'd', of a and b is also
the greatest common divisor of b and (a % b):
if d | a and d | b, then d | (a % b)
gcd(a, b) == gcd(b, (a % b))
gcd(b, (a % b)) == gcd((a % b), b % (a % b))
gcd((a % b), b % (a % b)) == gcd(b % (a % b), (a % b) % (b % (a % b)))
.
.
.
In essence, we continually reduce the integers down
like this, taking the 'b' of the pair, and taking the
remainder of a / b, until we reach a point at which
b | a. When we reach that point, the remainder is 0,
and therefore the dividend, b, is itself the greatest
common divisor.
Algorithm:
1. r := a % b
2. if r is 0, return abs(b)
3. a := b
4. b := r
5. go to 1 (recursively call gcd(b, r))
Example:
gcd(29, 17):
a = 29, b = 17
r = 29 % 17 == 12
r is not 0, continue
a = 17, b = 12
r = 17 % 12 == 5
r is not 0, continue
a = 12, b = 5
r = 12 % 5 == 2
r is not 0, continue
a = 5, b = 2
r = 5 % 2 == 1
r is not 0, continue
a = 2, b = 1
r = 2 % 1 == 0
r is 0, the result is the final divisor, 1
*/
/**
* Using C++11 lambda function
*/
static std::function<int(int,int)> gcd = [](int a, int b){
if (0 == b)
return 0;
if (0 == a)
return b;
const int r = a % b;
if (0 == r)
return abs(b);
return gcd(b, r);
};
/**
* C++11 lamda function for testing results
*
* The above gcd lambda must be captured in this scope
*/
static std::function<void(int,int,int)> gcd_tester = [gcd](int a, int b, int expected){
const int result = gcd(a, b);
std::cerr << "GCD(" << a << ", " << b << ") == " << expected << ": ";
if (result == expected)
std::cerr << ": PASSED" << std::endl;
else
std::cerr << ": FAILED (Got result " << result << ")" << std::endl;
};
int
main(void)
{
gcd_tester(16, 10, 2);
gcd_tester(17, 11, 1);
gcd_tester(461952, 116298, 18);
gcd_tester(7966496, 314080416, 32);
gcd_tester(24826148, 45296490, 526);
gcd_tester(12, 0, 0);
gcd_tester(0, 0, 0);
gcd_tester(0, 9, 9);
gcd_tester(-17, 11, 1);
gcd_tester(16, -10, 2);
gcd_tester(-16, -10, 2);
/*
Output from above:
GCD(16, 10) == 2: : PASSED
GCD(17, 11) == 1: : PASSED
GCD(461952, 116298) == 18: : PASSED
GCD(7966496, 314080416) == 32: : PASSED
GCD(24826148, 45296490) == 526: : PASSED
GCD(12, 0) == 0: : PASSED
GCD(0, 0) == 0: : PASSED
GCD(0, 9) == 9: : PASSED
GCD(-17, 11) == 1: : PASSED
GCD(16, -10) == 2: : PASSED
GCD(-16, -10) == 2: : PASSED
*/
return 0;
}