How long would this take?
int gcd(int a, int b) {
if (b==0) return a;
if (a<b) return gcd(b,a);
return gcd(b,a-b);
}
int gcd(int a, int b) {
if (b==0) return a;
if (a<b) return gcd(b,a);
return gcd(b,a % b);
}
Get rid of modulus: a mod b=a−⌊ab⌋×b
bx1+(a−⌊ab⌋y1)=g
Rearrange to get coefficients of a and b:
ay1+b(x1−⌊ab⌋y1)=g
void extendedEuclid(int a, int b, int &x, int &y, int &d) {
if (b == 0) {
x = 1; y = 0; d = a; return;
}
extendedEuclid(b, a % b);
int x1 = y;
int y1 = x - (a / b) * y;
x = x1;
y = y1;
}
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | ||
33 | 6 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | ||
33 | 6 | ||
6 | 3 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | ||
33 | 6 | ||
6 | 3 | ||
3 | 0 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | ||
33 | 6 | ||
6 | 3 | ||
3 | 0 | 1 | 0 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | ||
33 | 6 | ||
6 | 3 | 0 | 1 |
3 | 0 | 1 | 0 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | ||
33 | 6 | 1 | -5 |
6 | 3 | 0 | 1 |
3 | 0 | 1 | 0 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | -5 | 11 |
33 | 6 | 1 | -5 |
6 | 3 | 0 | 1 |
3 | 0 | 1 | 0 |
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?
a | b | x | y |
---|---|---|---|
72 | 33 | -5 | 11 |
33 | 6 | 1 | -5 |
6 | 3 | 0 | 1 |
3 | 0 | 1 | 0 |
72×−5+33×11=3
Suppose you go to the store. You buy x apples at 72 cents each and y oranges at 33 cents each. You spend $5.85. How many of each did you buy?