Advertisement
AquaBlitz11

GCD/LCM

Oct 19th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // euclidian algorithm
  5. unsigned gcd(unsigned u, unsigned v)
  6. {
  7.     while (v != 0)
  8.     {
  9.         unsigned r = u % v;
  10.         u = v;
  11.         v = r;
  12.     }
  13.     return u;
  14. }
  15.  
  16. // ab = (a, b)[a, b]
  17. // [a, b] = ab / (a, b)
  18. unsigned lcm(unsigned u, unsigned v)
  19. {
  20.     return (u / gcd(u, v)) * v;
  21. }
  22.  
  23. int main()
  24. {
  25.     int a, b, c;
  26.     cin >> a >> b >> c;
  27.     cout << gcd(gcd(a, b), c) << endl;
  28.     cout << lcm(lcm(a, b), c) << endl;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement