Skip to content

Commit 8215433

Browse files
committed
Merge pull request kennyledet#256 from ghostsnstuff/euclidean-cpp
euclidean algos - cpp implementations
2 parents a127208 + f432456 commit 8215433

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @author ghostsnstuff
3+
* @date 2/14/14
4+
* @description http://en.wikipedia.org/wiki/Euclidean_algorithm
5+
* @implementations division, subtraction, recursive
6+
*/
7+
8+
#include <iostream>
9+
using namespace std;
10+
11+
int gcd_division(int a, int b) {
12+
int temp = 0;
13+
while(b != 0) {
14+
temp = b;
15+
b = a % b;
16+
a = temp;
17+
}
18+
return a;
19+
}
20+
21+
int gcd_subtraction(int a, int b) {
22+
while(a != b) {
23+
if(a > b) {
24+
a -= b;
25+
} else {
26+
b -= a;
27+
}
28+
}
29+
return a;
30+
}
31+
32+
int gcd_recursive(int a, int b) {
33+
return (b == 0) ? a : gcd_recursive(b, a % b);
34+
}
35+
36+
int main(int argc, const char * argv[]) {
37+
cout << "gcd_division(28, 14) = " << gcd_division(28, 14) << endl;
38+
cout << "gcd_subtraction(28, 14) = " << gcd_subtraction(28, 14) << endl;
39+
cout << "gcd_recursive(28, 14) = " << gcd_recursive(28, 14) << endl;
40+
}

0 commit comments

Comments
 (0)