Advertisement
AquaBlitz11

13 gcd(a, b) = lcm(a, b); a + b = 2016

Oct 19th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6. Problem statement
  7. Given that a and b are 2 integers such tat 13 gcd(a,b) = lcm(a, b)
  8. and a + b = 2016, what are the values of a and b?
  9. */
  10.  
  11. // copied from other flie, great
  12. // euclidian algorithm
  13. unsigned gcd(unsigned u, unsigned v)
  14. {
  15.     while (v != 0)
  16.     {
  17.         unsigned r = u % v;
  18.         u = v;
  19.         v = r;
  20.     }
  21.     return u;
  22. }
  23.  
  24. // ab = (a, b)[a, b]
  25. // [a, b] = ab / (a, b)
  26. unsigned lcm(unsigned u, unsigned v)
  27. {
  28.     return (u / gcd(u, v)) * v;
  29. }
  30.  
  31. int main()
  32. {
  33.     for (int a = 1; a <= 2015; a++)
  34.     {
  35.         int b = 2016 - a;
  36.         if (13 * gcd(a, b) == lcm(a, b))
  37.         {
  38.             cout << a << " " << b << endl;
  39.             break;
  40.         }
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement