Advertisement
LEGEND2004

GCD LCM

Jun 20th, 2024
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #pragma GCC optimize("O3")
  2. #include <bits/stdc++.h>
  3. #include <numeric>
  4. using namespace std;
  5.  
  6. #define int long long
  7. #define double long double
  8. #define endl '\n'
  9. #define fastio ios_base::sync_with_stdio(0); cin.tie(0)
  10.  
  11.  
  12. int gcd(int a , int b){
  13.     if(b == 0)
  14.         return a;
  15.     return gcd(b , a % b);
  16. }
  17.  
  18.  
  19. int lcm(int a , int b){
  20.     return a / gcd(a , b) * b;
  21. }
  22.  
  23. signed main()
  24. {
  25.     fastio;
  26.     /*
  27.     GCD(24 , 18) = 6
  28.     LCM(24 , 18) = 72
  29.  
  30.     24 = 2 * 2 * 2 * 3
  31.     18 = 2 * 3 * 3
  32.  
  33.     GCD(24 , 18) = GCD(18 , 6) = GCD(12 , 6) = GCD(6 , 6) = GCD(0 , 6)
  34.     10 - 3 = 7 - 3 = 4 - 3 = 1
  35.  
  36.     GCD(a , b) = GCD(b , a % b)
  37.  
  38.     GCD(24 , 18) = GCD(18 , 6) = GCD(6 , 0)
  39.     */
  40.  
  41.     int a , b;
  42.     cin >> a >> b;
  43.     cout << __gcd(a , b) << " " << lcm(a , b);
  44.  
  45.  
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement