GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms_and_data_structures/math_problems/gcd.cpp at master · lualgorithm/algorithms_and_data_structures Expand file tree/ Copy pathgcd.cppMore file actions 60 lines (53 loc) · 1.34 KB / Copy pathgcd.cppFile metadata and controls60 lines (53 loc) · 1.34 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 57 58 59 60 /** * Given two numbers, determine the greatest common divisior of the two numbers. */ #include <iostream> int gcd1( int a, int b ) { while( b > 0 ) { int temp = a % b; a = b; b = temp; } return a; } int gcd2( int a, int b ) { if ( b == 0 ) return a; if ( a == 0 ) return b; return gcd2(b, a % b); } int gcd3( int a, int b ) { if ( a == 0 ) return b; if ( b == 0 ) return a; while ( a != b ) { if ( a > b ) { a = a-b; } else { b = b-a; } } return a; } int gcd4 ( int a, int b ) { if ( a == 0 ) return b; if ( b == 0 ) return a; if ( a == b ) return a; if ( a > b ) return gcd4(b, a-b); else return gcd4(a, b-a); } int main() { int a, b; std::cout << "Enter number 1 : "; std::cin >> a; std::cout << "Enter number 2 : "; std::cin >> b; std::cout << "GCD of " << a << " and " << b << " is " << gcd1(a,b) << std::endl; std::cout << "GCD of " << a << " and " << b << " is " << gcd2(a,b) << std::endl; std::cout << "GCD of " << a << " and " << b << " is " << gcd3(a,b) << std::endl; std::cout << "GCD of " << a << " and " << b << " is " << gcd4(a,b) << std::endl; return 0; } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |