0

Given two numbers $A$,$B$. Let $G$ be the GCD of two numbers. I need to tell the values of $X$ and $Y$ such that

$$ G = X A + Y B $$

How to approach this problem ? Like if we have $A=25$ and $B=45$ then GCD , $G=5$.

So $5 = 2 \times 25 - 1 \times 45$. Hence here $X=2$ and $Y=-1$.

So how to tackle this problem for given $A$ and $B$?

My try :

int a=25;
int b=45;
int s=0;
int old_s=1;
int t=1;
int old_t=0;
int r=b;    
int old_r=a;
while(r!=0){
    int quotient = old_r / r;
    old_r = r;
    r = old_r-quotient * r;
    old_s = s;
    s = old_s - quotient * s;
    old_t = t;
    t = old_t - quotient * t;
}
cout<< old_s << " " << old_t<<endl;
cout<< old_r <<endl;
cout<< t << " " << s <<endl;

Whats wrong with this code ?

mat7
  • 245

1 Answers1

0

One glance at your C snippet and I have no hope it is worth analysing, but don't take this as criticism. If I were good at C I would give you a routine that does the job, but I am not. So, I have a couple of pages in a .pdf file, page one shows Euclid Extended for a=3587, b=1819, and shows how gcd(3587,1819)=17 is computed, and how in one and the same run factors are computed so that -36*3587+71*1819=17. I plucked these numbers out of the sky of course, so the second page shows the whole process in abstract notation. Once you see those sheets it will all be as clear to you as it was to Euclid 2300 years ago, and you will be able to re-write your C routine. Problem is, how do I get this .pdf file to you? I tried pasting it into this box, but the formatting screws up hopelessly and I do not have the time to stuff around with formatting.