Advertisement
karbaev

EuclidX3

Oct 10th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. //1  GCD USING EUCLIDEAN ALGORITHM
  2. int euclid_gcd(int m, int n)
  3. {
  4.         int temp = 0;
  5.         if(m < n)
  6.         {
  7.                 temp = m;
  8.                 m = n;
  9.                 n = temp;
  10.         }
  11.         while(n != 0)
  12.         {
  13.                 temp = m % n;
  14.                 m = n;
  15.                 n = temp;
  16.         }
  17.         return m;
  18. }
  19.  
  20.  
  21.  
  22. //2 - как не надо делать;))
  23. #include<iostream>
  24. using namespace std;
  25. void finder()
  26. {
  27.     cout<<"Enter two numbers\n";
  28.     cin>>a>>b;
  29.     while(a!=b)//implementing euclidean algorith
  30.    
  31.     {
  32.         if(a>b)
  33.         a=a-b;//subtracting the lesser number
  34.         else
  35.         b=b-a;
  36.        
  37.     }
  38.     cout<<"GCD Of two numbers is "<<a<<endl;//to print can use a or b
  39. }
  40.  
  41. //3  recursive
  42. int gcd(int u, int v)
  43. {
  44.     return (v != 0) ? gcd(v, u % v) : u;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement