Created
September 28, 2018 13:15
-
-
Save rurumimic/3fc9bf784eb0c843cd83c4aba3ef8cff to your computer and use it in GitHub Desktop.
C++ gcd & lcm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int gcd(int p, int q) { | |
return q == 0 ? p : gcd(q, p % q); | |
} | |
int lcm(int p, int q) { | |
return p / gcd(p, q) * q; | |
} | |
int main(int argc, const char * argv[]) { | |
cout << gcd(6, 3) << endl; // 3 | |
cout << gcd(30, 18) << endl; // 6 | |
cout << gcd(17, 34) << endl; // 17 | |
cout << lcm(6, 2) << endl; // 6 | |
cout << lcm(30, 6) << endl; // 30 | |
cout << lcm(17, 11) << endl; // 187 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment