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 - \lfloor {a\over b}\rfloor \times b\)
\[bx_1 + (a-\lfloor{a\over b}\rfloor y_1) = g\]
Rearrange to get coefficients of \(a\) and \(b\):
\[ ay_1 + b(x_1 - \lfloor {a\over b}\rfloor y_1) = 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 \times -5 + 33 \times 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?