Advertisement
abrar1

Euclids theorem ax+by = gcd(a,b)

Nov 16th, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
  5. C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
  6. Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. //ax+ by = gcd(a,b)
  14.  
  15. // for a, b, find x,y, gcd(a,b)
  16.  
  17. // recursion x = y1 - floor(b/a)*x1
  18. // y= x1
  19.  
  20. int gcd(int a, int b, int& x, int& y){
  21. if(a == 0){
  22. x=0;
  23. y=1;
  24. return b;
  25. }
  26.  
  27. int x1, y1;
  28. int d = gcd(b%a, a, x1,y1);
  29. x = y1 - (b/a)*x1;
  30. y = x1 ;
  31. return d;
  32. }
  33.  
  34. int main()
  35. {
  36. int a,b;
  37. cin >> a;
  38. cin >> b ;
  39. if (a > b){
  40. int tmp = a;
  41. a = b;
  42. b = tmp;
  43. }
  44. int x, y, gcd;
  45.  
  46. cout << gcd(a,b,x,y) << ' '<< x << ' ' << y ;
  47.  
  48.  
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement