Last active
May 29, 2020 04:20
-
-
Save Ch-sriram/5456cca57a36d43a9d7eecc9c87844d6 to your computer and use it in GitHub Desktop.
Given two integers A and B, find their 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> | |
#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