Skip to content

Instantly share code, notes, and snippets.

@rurumimic
Created September 28, 2018 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rurumimic/3fc9bf784eb0c843cd83c4aba3ef8cff to your computer and use it in GitHub Desktop.
Save rurumimic/3fc9bf784eb0c843cd83c4aba3ef8cff to your computer and use it in GitHub Desktop.
C++ gcd & lcm
#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