File tree Expand file tree Collapse file tree
Euclidean_algorithm/C++/ghostsnstuff Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments