Advertisement
smatskevich

GCD

Feb 27th, 2021
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.36 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int GCD(int a, int b) {
  4.   if (a == 0 || b == 0) throw std::runtime_error("Zero params for GCD");
  5.  
  6.   // Алгоритм Евклида
  7.   while (true) {
  8.     int r = a % b;
  9.     if (r == 0) return b;
  10.     a = b;
  11.     b = r;
  12.   }
  13. }
  14.  
  15. int main() {
  16.   int a = 0, b = 0;
  17.   std::cin >> a >> b;
  18.  
  19.   std::cout << GCD(a, b);
  20.   return 0;
  21. }
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement