Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Last active May 29, 2020 04:20
Show Gist options
  • Save Ch-sriram/5456cca57a36d43a9d7eecc9c87844d6 to your computer and use it in GitHub Desktop.
Save Ch-sriram/5456cca57a36d43a9d7eecc9c87844d6 to your computer and use it in GitHub Desktop.
Given two integers A and B, find their GCD & LCM.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define u uint64_t
// Good explanation: https://www.youtube.com/watch?v=p5gn2hj51hs
u gcd_euclid(u a, u b) { return b == 0 ? a : gcd_euclid(b, a%b); }
u _lcm(u a, u b, u gcd) { return (a*b)/gcd; }
string solve(u a, u b) {
if (b > a) swap(a,b);
u gcd = gcd_euclid(a, b);
u lcm = _lcm(a, b, gcd);
stringstream ss;
ss << lcm << " " << gcd;
return ss.str();
}
int main() {
u t; cin >> t;
while(t--) {
u a, b; cin >> a >> b;
cout << solve(a,b) << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment